Matplotlib 笔记
1 | import matplotlib.pyplot as plt |
简单创建图象
1 | fig = plt.figure(figsize=(1,1)) # 创建一个图,并指定长宽(1inch = 80px) |
- 一张 figure 可以有很多 axes
- axes 即为图象部分
- 一个 axes 有两个 axies(座标轴)
- 通过 fig.axes 属性获取已创建 axes
- 通过 ax.lines 属性获取已创建 lines
- 删除 axes 用
delaxes
方法 - 增加共用 x 轴的 y 轴,用
ax.twinx
方法ax_same_x._get_lines.prop_cycler = ax._get_lines.prop_cycler
解决颜色重合ax_another_same_x.spines['right'].set_position(('axes', 1.1))
将更多的 y 轴右移以防重合
标识
-
轴名
1
2ax.set_xlabel('my xdata')
ax.set_ylabel('my ydata') -
标题
1
2ax.set_title('my axe title')
fig.suptitle('my figure title') -
网格
1
ax.grid(True)
Legend 图例
1 | line.set_label('Label via method') # 设置图象的图例名 |
loc
指定位置bbox_to_anchor
指定盒模型锚点及长宽 (x, y, width, height)ncol
列数
交互模式
1 | plt.ioff() # 关闭交互模式 |