[python]matplotlib.pyplot模块

参考文章

https://www.jianshu.com/p/bf8233840687

基本概念

1、figure

用画板和画纸来做比喻的话,figure就好像是画板,是画纸的载体,但是具体画画等操作是在画纸上完成的。在pyplot中,画纸的概念对应的就是Axes/Subplot。

2、axes 和 subplot区别
  • subplot

add_subplot()主要是用于绘制多张图的排版。

里面传入的三个数字,前两个数字代表要生成几行几列的子图矩阵,最后一个个数字代表选中的子图位置。

举个例子:

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
plt.show()

在这里插入图片描述

  • axes

add_aexs()同分割子图的概念不同,类似于图中图,暂时没有机会用到就不深入展开了。

括号里面的值前两个是轴域原点坐标(从左下角计算的),后两个是显示坐标轴的长度。

fig = plt.figure()
ax3 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax4 = fig.add_axes([0.72, 0.72, 0.16, 0.16])
plt.show()

在这里插入图片描述

两种绘图方法

pyplot中包含了函数式编程和面向对象编程的两种方法,可交替使用。

1、函数式绘图
plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)
plt.xlim(0.5, 4.5)
plt.show()


以该图为例,调用了plot()xlim()函数

2、面向对象式绘图
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)
ax.set_xlim(0.5, 4.5)
plt.show()

猜你喜欢

转载自blog.csdn.net/TOMOCAT/article/details/87809497