当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python matplotlib SubFigure.subplots用法及代码示例


本文简要介绍 python 语言中 matplotlib.figure.SubFigure.subplots 的用法。

用法

subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, width_ratios=None, height_ratios=None, subplot_kw=None, gridspec_kw=None)

向该图中添加一组子图。

这个实用程序包装器可以方便地在单个调用中创建子图的通用布局。

参数
nrows, ncols 整数,默认值:1

子图网格的行/列数。

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

控制 x 轴 (sharex) 或 y 轴 (sharey) 的共享:

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

  • 错误或'none':每个子图的 x 轴或 y 轴都是独立的。

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

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

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

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

squeeze 布尔值,默认值:真
  • 如果为 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 字典,可选

带有传递给 Figure.add_subplot 调用的关键字的字典,用于创建每个子图。

gridspec_kw 字典,可选

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

返回
Axes 或轴数组

如果创建了多个子图,则可以是单个 Axes 对象或 Axes 对象数组。结果数组的维度可以使用squeeze 关键字来控制,见上文。

例子

# First create some toy data:
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)

# Create a figure
fig = plt.figure()

# Create a subplot
ax = fig.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')

# Create two subplots and unpack the output array immediately
ax1, ax2 = fig.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
axes = fig.subplots(2, 2, subplot_kw=dict(projection='polar'))
axes[0, 0].plot(x, y)
axes[1, 1].scatter(x, y)

# Share an X-axis with each column of subplots
fig.subplots(2, 2, sharex='col')

# Share a Y-axis with each row of subplots
fig.subplots(2, 2, sharey='row')

# Share both X- and Y-axes with all subplots
fig.subplots(2, 2, sharex='all', sharey='all')

# Note that this is the same as
fig.subplots(2, 2, sharex=True, sharey=True)

相关用法


注:本文由纯净天空筛选整理自skytowner.com大神的英文原创作品 matplotlib.figure.SubFigure.subplots。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。