当前有效matplotlib
版本为:3.4.1
。
概述
subplots
函数的功能是创建一个figure
对象和一组子图。
函数的定义签名为:matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
函数的参数为:
nrows, ncols
: 子图网格的行/列数。可选参数。整数,默认值为1
。sharex, sharey
: 控制子图共享x或y轴的行为。布尔值或{'none', 'all', 'row', 'col'}
,默认值为False
。True
or'all'
:所有子图共享x
/y
轴False
or'none'
:所有子图的x
/y
轴都是独立的。'row'
:每行子图共享x
/y
轴'col'
:每列子图共享x
/y
轴
squeeze
:控制函数返回值中子图元组的维度。可选参数。布尔值,默认值为True
。- 值为
False
:函数返回值为Axes
对象二维数组。 - 值为
True
:压缩函数返回值中~matplotlib.axes.Axes
对象的维度。- 子图数量为
1
,返回一个单独的Axes
对象(标量)。 - 子图结构为
Nx1
或1xM
,返回一个Axes
对象的一维数组。 - 子图结构为
NxM
,返回一个Axes
对象的二维数组。
- 子图数量为
- 值为
num
:用于设置figure
对象的编号或标签,属于.pyplot.figure
的关键字参数。可选参数。整数或字符串。默认值为None
。subplot_kw
:用于向创建子图时的底层函数~matplotlib.figure.Figure.add_subplot
传递关键字参数。可选参数。字典。gridspec_kw
:用于向创建子图网格时用到的~matplotlib.gridspec.GridSpec
类的构造函数传递关键字参数。可选参数。字典。**fig_kw
:用于向.pyplot.figure
传递关键字参数。可选参数。字典。
函数的返回值为二元组,构成元素为:
fig
:~.figure.Figure
对象ax
:.axes.Axes
对象或Axes
对象数组。
subplots
函数其实是Figure.subplots
方法的封装。源码为:
def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
subplot_kw=None, gridspec_kw=None, **fig_kw):
fig = figure(**fig_kw)
axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
squeeze=squeeze, subplot_kw=subplot_kw,
gridspec_kw=gridspec_kw)
return fig, axs
案例:演示squeeze
参数对函数返回值的影响
import matplotlib.pyplot as plt
# 绘制1个子图
r1=plt.subplots()
print(r1)
# 绘制1个子图,但squeeze为False
# 子图部分为1个Axes对象二维数组,形状为(1, 1)。
r2=plt.subplots(squeeze=False)
print(r2)
# 绘制3行2列6个子图
# 子图部分为1个Axes对象二维数组,形状为(3, 2)。
r3=plt.subplots(nrows=3,ncols=2,facecolor='r',subplot_kw=dict(facecolor="b"))
print(r3)
plt.show()
输出为:
# 子图部分为1个单独的Axes对象。
(<Figure size 432x288 with 1 Axes>, <AxesSubplot:>)
# 子图部分为1个Axes对象二维数组,形状为(1, 1)。
(<Figure size 432x288 with 1 Axes>, array([[<AxesSubplot:>]], dtype=object))
# 子图部分为1个Axes对象二维数组,形状为(3, 2)
(<Figure size 432x288 with 6 Axes>, array([[<AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>],
[<AxesSubplot:>, <AxesSubplot:>]], dtype=object))
案例:演示函数返回值的应用
这部分内容其实与matplotlib
无关,应该属于Python
和numpy
的基础知识。
subplots函数的返回值为2元组,一个元素为figure
对象,一个元素为Axes
对象。
典型应用方式如下,函数返回值中Axes
对象结构不同导致了这些不同写法。
# ax为标量
fig, ax = plt.subplots()
# (ax1, ax2)构成元组
fig, (ax1, ax2) = plt.subplots(1, 2)
# axes为二维数组
fig, axes = plt.subplots(2, 2)
axes[0, 0].plot(x, y)
axes[1, 1].scatter(x, y)
案例:演示sharex、sharey参数的取值
import matplotlib.pyplot as plt
# Share a X axis with each column of subplots
plt.subplots(2, 2, sharex='col')
plt.suptitle("sharex='col'")
# Share a Y axis with each row of subplots
plt.subplots(2, 2, sharey='row')
plt.suptitle("sharey='row'")
# Share both X and Y axes with all subplots
plt.subplots(2, 2, sharex='all', sharey='all')
plt.suptitle("sharex='all',sharey='all'")
# Note that this is the same as
plt.subplots(2, 2, sharex=True, sharey=True)
plt.suptitle("sharex=True, sharey=True")
plt.show()
案例:subplots
函数简易用法
import matplotlib.pyplot as plt
# 添加3行3列子图9个子图
fig, axes = plt.subplots(3, 3)
# 为第1个子图绘制图形
axes[0, 0].bar(range(1, 4), range(1, 4))
# 为第5个子图绘制图形
axes[1, 1].pie([4, 5, 6])
# 为第9个子图绘制图形
axes[2, 2].plot([1], 'o')
# 删除没有用到的子图
for i in range(3):
for j in range(3):
if i != j:
axes[i, j].remove()
plt.show()
subplot
函数与subplots
函数的对比
功能:
subplot
函数:每次绘制1个子图,多次调用可根据网格生成子图集合。subplots
函数:每次绘制1组子图,生成一个figure
对象(独立的图像)。
返回值:
subplot
函数:一个单独的Axes
对象。subplots
函数:元组结构,figure
对象和Axes
对象数组或Axes
对象。