matplotlib.pyplot.subplots#

matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, width_ratios=None, height_ratios=None, subplot_kw=None, gridspec_kw=None, **fig_kw)[源代码][源代码]#

创建一个图形和一组子图。

这个实用程序包装器使得通过一次调用即可方便地创建常见的子图布局,包括包含的图形对象。

参数:
nrows, ncolsint, 默认值: 1

子图网格的行数/列数。

sharex, shareybool 或 {'none', 'all', 'row', 'col'},默认值:False

控制 x 轴(sharex)或 y 轴(sharey)之间属性的共享:

  • True 或 'all':所有子图将共享 x 轴或 y 轴。

  • False 或 'none':每个子图的 x 轴或 y 轴将是独立的。

  • 'row': 每个子图行将共享一个 x 或 y 轴。

  • 'col': 每个子图列将共享一个 x 或 y 轴。

当子图在列中共享 x 轴时,只有底部子图的 x 刻度标签会被创建。类似地,当子图在行中共享 y 轴时,只有第一列子图的 y 刻度标签会被创建。要稍后打开其他子图的刻度标签,请使用 tick_params

当子图共享一个具有单位的轴时,调用 Axis.set_units 将使用新单位更新每个轴。

请注意,无法取消共享轴。

挤压bool, 默认值: True
  • 如果为 True,额外的维度将从返回的 Axes 数组中被压缩掉:

    • 如果只构建了一个子图(nrows=ncols=1),则返回的单个 Axes 对象作为标量返回。

    • 对于 Nx1 或 1xM 的子图,返回的对象是一个 Axes 对象的一维 numpy 对象数组。

    • 对于 NxM,当 N>1 且 M>1 时,子图将作为二维数组返回。

  • 如果为 False,则不会进行任何压缩:返回的 Axes 对象始终是一个包含 Axes 实例的 2D 数组,即使它最终是 1x1。

width_ratios : 长度为 ncols 的类数组对象, 可选长度为的类数组对象

定义列的相对宽度。每列的相对宽度为 width_ratios[i] / sum(width_ratios)。如果未指定,所有列将具有相同的宽度。等同于 gridspec_kw={'width_ratios': [...]}

height_ratios : 长度为 nrows 的类数组对象, 可选长度为的类数组对象

定义行的相对高度。每行的高度为 height_ratios[i] / sum(height_ratios)。如果没有给出,所有行将具有相同的高度。这是 gridspec_kw={'height_ratios': [...]} 的便利用法。

subplot_kw字典, 可选

传递给 add_subplot 调用的关键字参数字典,用于创建每个子图。

gridspec_kw字典, 可选

传递给 GridSpec 构造函数的带有关键字的字典,用于创建子图放置的网格。

**fig_kw

所有额外的关键字参数都会传递给 pyplot.figure 调用。

返回:
fig
ax : Axes 或 Axes 数组Axes 或 Axes 数组

ax 可以是单个 Axes 对象,或者如果创建了多个子图,则可以是 Axes 对象的数组。结果数组的维度可以通过 squeeze 关键字控制,如上所述。

处理返回值的典型惯用法是:

# 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.subplots(1, 2)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

名称 ax 及其复数形式 axsaxes 更受欢迎,因为后者不清楚它是指单个 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(projection="polar"))
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)

使用 matplotlib.pyplot.subplots 的示例#

Pong

Pong

多进程

Multiprocessing

日期刻度定位器和格式化器

Date tick locators and formatters

时间序列的自定义刻度格式化器

Custom tick formatter for time series

弧度刻度

Radian ticks

使用 GTK3 的 pyplot

pyplot with GTK3

使用 GTK4 的 pyplot

pyplot with GTK4

SVG 工具提示

SVG Tooltip

Lasso 选择器

Lasso Selector