matplotlib.pyplot.subplots¶
-
matplotlib.pyplot.
subplots
(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)[源代码]¶ 创建一个数字和一组子批次。
这个实用程序包装器使在一次调用中创建子块的公共布局变得方便,包括封闭的图形对象。
参数: - 恩罗int,默认值:1
子批次网格的行/列数。
- 斯莱西bool或'none'、'all'、'row'、'col',默认值:false
控制x之间的属性共享( 股票 )或y( 共享 轴:
- “全部”或“全部”:X轴或Y轴将在所有子批次之间共享。
- 假或“无”:每个子批次X轴或Y轴都是独立的。
- “row”:每个子批次行将共享一个X轴或Y轴。
- “col”:每个子批次列将共享一个x轴或y轴。
当子批次沿列具有共享的X轴时,仅创建底部子批次的X记号标签。同样,当子批次沿行具有共享的Y轴时,只创建第一列子批次的Y记号标签。要稍后打开其他子批次的标记标签,请使用
tick_params
.- squeezebool,默认值:True
- 如果为真,则从返回的数组中挤出额外的维度
Axes
:- 如果只构造了一个子块(nrows=ncols=1),则生成的单轴对象将作为标量返回。
- 对于nx1或1xm子批次,返回的对象是轴对象的1d numpy对象数组。
- 对于nxm,n>1和m>1的子批次作为二维数组返回。
- 如果为false,则根本不进行压缩:返回的axes对象始终是包含axes实例的2d数组,即使它最终是1x1。
- 如果为真,则从返回的数组中挤出额外的维度
- subplot_kw可选的
将关键字传递给
add_subplot
用于创建每个子批次的调用。- gridspec_kw可选的
将关键字传递给
GridSpec
用于创建子批次所在的网格的构造函数。- **fig_kw
所有其他关键字参数都传递给
pyplot.figure
打电话。
返回: - fig :
Figure
图形 - ax :
axes.Axes
或轴阵列轴线。轴线或轴阵列 ax 可以是单人间
Axes
如果创建了多个子批次,则为对象或轴对象数组。结果数组的尺寸可以用squence关键字控制,见上文。处理返回值的典型习惯用法有:
# using the variable ax for single a Axes fig, ax = plt.subplots() # using the variable axs for multiple Axes fig, axs = plt.subplots(2, 2) # using tuple unpacking for multiple Axes fig, (ax1, ax2) = plt.subplot(1, 2) fig, ((ax1, ax2), (ax3, ax4)) = plt.subplot(2, 2)
名字
ax
多元化axs
优先于axes
因为对于后者,不清楚它是否指的是单一的Axes
实例或这些实例的集合。
实例
# First create some toy data: x = np.linspace(0, 2*np.pi, 400) y = np.sin(x**2) # Create just a figure and only one subplot fig, ax = plt.subplots() ax.plot(x, y) ax.set_title('Simple plot') # Create two subplots and unpack the output array immediately f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot(x, y) ax1.set_title('Sharing Y axis') ax2.scatter(x, y) # Create four polar axes and access them through the returned array fig, axs = plt.subplots(2, 2, subplot_kw=dict(polar=True)) axs[0, 0].plot(x, y) axs[1, 1].scatter(x, y) # Share a X axis with each column of subplots plt.subplots(2, 2, sharex='col') # Share a Y axis with each row of subplots plt.subplots(2, 2, sharey='row') # Share both X and Y axes with all subplots plt.subplots(2, 2, sharex='all', sharey='all') # Note that this is the same as plt.subplots(2, 2, sharex=True, sharey=True) # Create figure number 10 with a single subplot # and clears it if it already exists. fig, ax = plt.subplots(num=10, clear=True)