- 学习matplotlib画图已经有一段时间了,各种操作都做了一遍,但是还是有点迷糊,尤其是座标轴上各个部分的名称,在此特意将matplotlib上的经典例图的各部分名称全部总结在此,做个总结,以后再做操作上补充。
主要图像要素的名称
- Title
- 图名(在整个图的上面)
- label
- 标签(在座标轴的边上)
- 应用
- 座标轴标签自适应
- fig.autofmt_xdate()
- 设置y轴标签
- ax1.set_ylabel(‘Y1’)
- 座标轴标签自适应
- tick
- 刻度
- 设置座标轴有多少格
- ax.locator_params(‘x’, nbins = 10) # 设置x轴有10格组成
- legend
- 图例
- 应用
- plt.legend(loc = 0, ncol = 3) # 0代表最佳位置,3表示有三列
- ax.legend()
- grid
- 背景网格
- 应用
- plt.grid(True)
- ax.grid(True)
- text
- 文字注释(可以用TeX公式)
- 应用
- plt.text()
- ax.text(2,4,r"$ \alpha_i \beta_j \pi \lambda \omega $", size = 25)
- axis
- 座标轴
- xmin, xmax, ymin, ymax = axis()
- xmin, xmax, ymin, ymax = axis([xmin, xmax, ymin, ymax])
- xmin, xmax, ymin, ymax = axis(option)
- xmin, xmax, ymin, ymax = axis(**kwargs)
- 参数可以为下列单词
============================================================
Value | Description |
---|---|
‘on’ | Turn on axis lines and labels. Same as True . |
‘off’ | Turn off axis lines and labels. Same as False . |
‘equal’ | Set equal scaling (i.e., make circles circular) bychanging axis limits. |
‘scaled’ | Set equal scaling (i.e., make circles circular) by changing dimensions of the plot box. |
‘tight’ | Set limits just large enough to show all data. |
‘auto’ | Automatic scaling (fill plot box with data). |
‘normal’ | Same as ‘auto’; deprecated. |
‘image’ | ‘scaled’ with axis limits equal to data limits. |
‘square’ | Square plot; similar to ‘scaled’, but initially forcing xmax-xmin = ymax-ymin |
===========================================================
-
应用
- 可以自己定制座标范围
- plt.axis([-100,100,-10,200])
- 单独调节某个参数
- plt.xlim([-5,5])
- 添加座标轴
- plt.twinx()
- 可以自己定制座标范围
-
figure
- 画板,是画纸的载体
-
https://www.zhihu.com/question/51745620
-
Axes/Subplot
-
画纸 轴域 ~=子图
-
axes(轴域)可以理解一些轴的集合:axes不是数学上的座标轴,而是图形结构中的一个对象。所有绘图函数都是直接作用在当前axes对象上。
-
subplot子图就是画板上的画纸
-
应用
- plt.subplot(221)
- fig = plt.figure()
- fig.add_axes([0.1, 0.1, 0.8, 0.8])
- fig.add_subplot(111)
-
https://www.zhihu.com/question/51745620(这里面还有作者对于面向对象和面向过程编程的思考和建议哦)
-
-
spines
- 轴脊线-记录数据区域边界的线
- gca
- get current axes
- 获取图像的轴,总共有四个轴top、bottom、left和right
- plt.spines[‘top’]
- …
- plt.spines[‘top’].set_color()
- plt.spines[‘right’].set_color(‘none’)(隐藏座标轴)
- …
- plt.spines[‘top’].set_position()
主要函数的参数
- color
- 颜色
- marker
- 点型
-
- linestyle
- 线型
- linewidth
- 线宽
- loc
- 位置
- ncol
- 列
- nbins
- 格数
- size
- 大小
- family
- 字体
- style
- 字体风格
- bbox
- 边框
- alpha
- 透明度
- weight
- 粗细