【PY从0到1】数据可视化1

# tushare ID: 409200
# 数据可视化1

# 导入库
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

#设置美化参数
sns.set()

# 数据准备
np.random.seed(10)
a = np.random.randn(100,2) # 生成随机数
df = pd.DataFrame(a) # 转化为DataFrame
dates = pd.date_range('2021-2-9',periods=len(a),freq='D')
df.columns = ['Data1','Data2']
df.index = dates
print(df.head()) 
# 数据准备完毕,打印查看结果。
#                Data1     Data2
# 2021-02-09  1.331587  0.715279
# 2021-02-10 -1.545400 -0.008384
# 2021-02-11  0.621336 -0.720086
# 2021-02-12  0.265512  0.108549
# 2021-02-13  0.004291 -0.174600

# 1>可视化(绘制的数据在一个DataFrame中)
# ①生成简单的图
df.plot(figsize=(8,6),title='numbers') # figsize是图片尺寸,title是标题。

# ②设置y轴范围
plt.figure() # 重置画布。
df['Data2'].plot(figsize = (8,6),
                 ylim=[np.min(df.Data2)*1.2,
                       np.max(df.Data2)*1.2]) # 单独画Data2,ylim为y轴范围。

# ③子图模式
df[['Data1','Data2']].plot(subplots=True,figsize=(8,6))
  
# ④直方图
df.hist(figsize = (10,4), bins = 10)

# ⑤散点图(非常重要)
df.plot(x='Data1',
        y='Data2',
        kind='scatter',
        figsize=(8,6),
        title='Scatter plot')

# ⑥累计求和图
df.cumsum().plot(figsize=(8,6))

# 2> 调整画图风格
df.plot(style=['o-', 'm--'],
        figsize = (10,6)) # o代表点,-代表线

# 3> 生成的图包括两个y轴
df.plot(figsize=(10,8),secondary_y='Data2',style='--')

 

posted @ 2021-04-26 21:07  泥鳅不怕水  阅读(71)  评论(0)    收藏  举报