matplotlib.pyplot(plt)
- add_subplot()的作用以及plt的初步理解
import matplotlib.pyplot as plt
fig1 = plt.figure('1') # 定义一个总图fig1,图像标题更改为'1',否则默认为'Figure 1'
fig2 = plt.figure('2') # 图像标题更改为'2'
fig3 = plt.figure('3') # 图像标题更改为'3'
# 参数:add_subplot(row, column, index)
# 这里的row和column指将总图fig划分为几行几列
# index指代子图的序号及其在总图中的位置
# 所以index的最大值为row*column
fig1.add_subplot(111)
fig2.add_subplot(221)
fig3.add_subplot(339)
plt.show() # 打印图片
# 最后会输出三个总图“1”、“2”、“3”,各带有一个子图
# fig3中的子图会显示在总图的右下角
import matplotlib.pyplot as plt
fig = plt.figure('1') # 定义一个总图fig1,并将图像标题更改为'1',否则默认为'Figure 1'
ax1 = plt.subplots(2, 2) # 重新建立一个总图再添加2*2的子图
fig, ax2 = plt.subplots(2, 2) # 效果同上,不太清楚加fig, 的作用
plt.show() # 打印图片
# 最后会输出三个总图
- 一定要注意plt.subplots()和plt.subplot(),前者多一个字母“s”,会新建一张总图,而后者不会
import matplotlib.pyplot as plt
fig = plt.figure('1')
ax = plt.subplot(231)
ax = plt.subplot(232)
ax = plt.subplot(233)
ax = plt.subplot(234)
ax = plt.subplot(235)
ax = plt.subplot(236)
plt.show() # 打印图片
# 最后会输出一张总图“1”,并有六张顺序排列的子图
fig = plt.figure('1')
ax = plt.subplot(231)
ax = plt.subplot(232)
# 注意:以下使用了一个plt.subplots
# 一旦使用plt.subplots,就会创建一张新的总图,并且其只有两个参数
# 两个参数一定要用“,”分隔
ax = plt.subplots(2, 3) # 会产生一张铺满子图的新图
# 下面的图会覆盖plt.subplots(2, 3)中的子图
ax = plt.subplot(234)
ax = plt.subplot(235)
ax = plt.subplot(236)
plt.show()