Python模块 - matplotlib
matplotlib 简介
matplotlib是一个强大的python绘图和数据可视化的工具包。
安装方法: pip install matplotlib
引用方法: import matplotlib.pyplot as plt
绘图函数: plt.plot()
显示图像: plt.show()
matplotlib.plot 函数
plot函数:绘制点图或线图
- 线型:linestyle(-, -., --, : )
- 点型:marker(v, ^, S, *, H, +, D, o, ...)
- 颜色:color(b, g, r, y, k, w, ...)
plot函数绘制多条曲线
plt.plot([1, 2, 3, 4], [2, 3, 1, 7], color='red', label='Line A') # 折线图
plt.plot([1, 2, 3, 4], [3, 5, 6, 9], color='black', marker='o', label='Line B')
plt.title("折线图的标题")
plt.xlabel("折线图X轴的标题")
plt.ylabel("折线图Y轴的标题")
# plt.xlim(0, 10)
# plt.ylim(0, 10)
# plt.xticks(np.arange(0, 11, 2))
plt.legend()
plt.show()
matplotlib 图像标注
| 设置 | 方法 |
|---|---|
| 设置图像标题 | plt.title() |
| 设置x轴名称 | plt.xlabel() |
| 设置y轴名称 | plt.ylabel() |
| 设置x轴范围 | plt.xlim() |
| 设置y轴范围 | plt.ylim() |
| 设置x轴刻度 | plt.xticks() |
| 设置y轴刻度 | plt.yticks() |
| 设置曲线图例 | plt.legend() |
matplotlib 画布与子图
- 画布:figure
fig = plt.figure() - m子图:subplot
ax1 = fig.add_subplot(2, 2, 1) - 调节子图间距:
subplots_adjust(left, bottom, right, top, wspace, hspace)
matplotlib 柱状图
data = [32, 48, 21, 100]
labels = ['Jan','Feb','Mar','Apr']
plt.bar(np.arange(len(data)),data, align='edge')
plt.xticks(np.arange(len(data)), labels)
plt.show()
matplotlib 饼图
plt.pie([10,20,30,40], labels=['a','b','c','d'], autopct="%.2f%%", explode=[0.1,0,0.1,0])
plt.axis('equal')
plt.show()
matplotlib-finance 画图(金融相关)
import matplotlib.finance as fin
from matplotlib.dates import date2num
df['time'] = date2num(df.index.to_pydatetime())
df
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
arr = df[['time','open','close','high','low']].values
fin.candlestick_ochl(ax, arr)

浙公网安备 33010602011771号