Matplotlib是一个强大的Python绘图和数据可视化的工具包。
安装方法:pip install matplotlib
引用方法:import matplotlib.pyplot as plt
绘图函数:plt.plot()
显示图像:plt.show()
-, -. ,-- , ...v, ^, S, *, H, +, x, D, o, ...b, g, r, y, k, w, ...# 线的表示
plt.plot([1,2,5,8], [3,4,5,9], "r", label=‘line a‘)# 折线图(X, Y)
plt.plot([1,2,4,8], [1,3,7,10], color="y", label=‘line b‘)# 折线图(X, Y)
plt.legend()
plt.show()
df = pd.read_csv(‘zn2006SHFE2020.csv‘, index_col=‘datetime‘, parse_dates=[‘datetime‘])[[‘open‘,‘high‘,‘low‘,‘close‘]]
df.plot()
plt.show()
plt.title()plt.xlabel()plt.ylabel()plt.xlim()plt.ylim()plt.xticks()plt.yticks()plt.legend()# 使用Matplotlib模块在一个窗口中绘制数学函数 y=x, y=x2, y=3x**3+5x**2+2x+1的图像,使用不同颜色的线加以区别,并使用图例说明各个线代表说明函数
x = np.linspace(-1000, 1000, 10000)
y1 = x.copy()
y2 = x**2
y3 = 3*x**3+5*x**2+2*x+1
plt.plot(x, y1,‘y‘, label=‘y=x‘)
plt.plot(x, y2,‘r‘, label=‘y2‘)
plt.plot(x, y3,‘b‘, label=‘y3‘)
plt.xlim(-1000, 1000)
plt.ylim(-1000, 1000)
plt.legend()
plt.show
在一个画布里画很多图
画布 figure
fig = plt.figure()
图 subplot
ax1=fig.add_subplot(2,2,1)
调解子图间距
subplots_adjust(left, bottom, right, top, wspace, hspace)
# 画图与子图
fig = plt.figure() # 添加一个画布
ax1 = fig.add_subplot(2,1,1) # 添加一个子图 2行1列 第1个位置
ax2 = fig.add_subplot(2,1,2) # 添加一个子图 2行1列 第2个位置
ax1.plot([1,2,4], [3,5,7])
ax2.plot([1,4,7], [3,2,7])
plt.show()
折线图
# 线的表示
plt.plot([1,2,5,8], [3,4,5,9], "r", label=‘line a‘)# 折线图(X, Y)
plt.plot([1,2,4,8], [1,3,7,10], color="y", label=‘line b‘)# 折线图(X, Y)
plt.legend()
plt.show()
条形图
# 条形图
plt.bar([1,2,3,5],[5,6,7,8])
plt.show()
# 条形图2
data= [56, 34, 22, 38]
labels = [‘Jan‘,‘Feb‘,‘Mar‘,‘Apr‘]
plt.bar(labels, data, color=‘y‘)
plt.show()
饼图
# 饼图
plt.pie([10,20,54,99],labels=[‘a‘,‘b‘,‘c‘,‘d‘], autopct=‘%.2f%%‘,explode=[0.2,0,0.1,0])
plt.show()
mplfinance中有许多绘制金融相关图的函数接口
绘制K线图:mplfinance.candlestick_ochl函数
原文:https://www.cnblogs.com/sunch/p/13221204.html