0. 前期准备:
导入三个必备的库,推荐使用jupyter notebook或者spyder编程环境
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
1. 线形图
1) Series直接生成线形图
参数介绍:
Series.plot()
:series的index为横坐标,value为纵坐标
kind
→ line,bar,barh…(折线图,柱状图,柱状图-横…)
label
→ 图例标签,Dataframe格式以列名为label
style
→ 风格字符串,这里包括了linestyle(-),marker(.),color(g)
color
→ 颜色,有color指定时候,以color颜色为准
alpha
→ 透明度,0-1
use_index
→ 将索引用为刻度标签,默认为True
rot
→ 旋转刻度标签,0-360
grid
→ 显示网格,一般直接用plt.grid
xlim,ylim
→ x,y轴界限
xticks,yticks
→ x,y轴刻度值
figsize
→ 图像大小
title
→ 图名
legend
→ 是否显示图例,一般直接用plt.legend()
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2020', periods=1000))
ts = ts.cumsum()
ts.plot(kind='line',
label = 'demo',
style = '--g.',
color = 'red',
alpha = 0.4,
use_index = True,
rot = 45,
#grid = True,
ylim = [-50,50],
yticks = list(range(-50,50,10)),
figsize = (8,4),
title = 'test',
legend = True)
–> 输出的结果为:(网格一些基础设置之前已经设置过了)
2)Dataframe直接生成图表
参数介绍:
subplots
→ 是否将各个列绘制到不同图表,默认False
colormap
→ 因为是默认按列进行绘图,所以有个colormap参数可以设置,具体的可取样式,可以参照上个博客的讲解方式
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
df.plot(kind='line',
style = '--.',
alpha = 0.4,
use_index = True,
rot = 45,
grid = True,
figsize = (8,4),
title = 'test',
legend = True,
subplots = False,
colormap = 'jet')
–> 输出的结果为:(可以试试
subplots = True
的情况)