使用 plt.subplots 创建多个子图

您所在的位置:网站首页 如何给柱形图命名图层 使用 plt.subplots 创建多个子图

使用 plt.subplots 创建多个子图

2024-07-13 12:46| 来源: 网络整理| 查看: 265

使用#创建多个子图plt.subplots

pyplot.subplots通过一次调用创建一个图形和一个子图网格,同时提供对如何创建单个图的合理控制。对于更高级的用例,您可以使用GridSpec更通用的子图布局或Figure.add_subplot在图中的任意位置添加子图。

import matplotlib.pyplot as plt import numpy as np # Some example data to display x = np.linspace(0, 2 * np.pi, 400) y = np.sin(x ** 2) 只有一个子图的图形#

subplots()不带参数返回 aFigure和一个 Axes.

这实际上是创建单个图形和轴的最简单和推荐的方法。

fig, ax = plt.subplots() ax.plot(x, y) ax.set_title('A single plot') Text(0.5, 1.0, 'A single plot') 在一个方向上堆叠子图#

的前两个可选参数pyplot.subplots定义子图网格的行数和列数。

仅在一个方向上堆叠时,返回axs的是一个 1D numpy 数组,其中包含创建的 Axes 列表。

fig, axs = plt.subplots(2) fig.suptitle('Vertically stacked subplots') axs[0].plot(x, y) axs[1].plot(x, -y) []

如果您只创建几个 Axes,立即将它们解压缩到每个 Axes 的专用变量会很方便。这样,我们可以使用ax1而不是更详细的axs[0].

fig, (ax1, ax2) = plt.subplots(2) fig.suptitle('Vertically stacked subplots') ax1.plot(x, y) ax2.plot(x, -y) []

要获得并排的子图,请传递一行和两列的参数。1, 2

fig, (ax1, ax2) = plt.subplots(1, 2) fig.suptitle('Horizontally stacked subplots') ax1.plot(x, y) ax2.plot(x, -y) [] 在两个方向上堆叠子图#

在两个方向堆叠时,返回axs的是一个 2D NumPy 数组。

如果您必须为每个子图设置参数,则可以方便地使用.for ax in axs.flat:

fig, axs = plt.subplots(2, 2) axs[0, 0].plot(x, y) axs[0, 0].set_title('Axis [0, 0]') axs[0, 1].plot(x, y, 'tab:orange') axs[0, 1].set_title('Axis [0, 1]') axs[1, 0].plot(x, -y, 'tab:green') axs[1, 0].set_title('Axis [1, 0]') axs[1, 1].plot(x, -y, 'tab:red') axs[1, 1].set_title('Axis [1, 1]') for ax in axs.flat: ax.set(xlabel='x-label', ylabel='y-label') # Hide x labels and tick labels for top plots and y ticks for right plots. for ax in axs.flat: ax.label_outer()

您也可以在 2D 中使用元组解包将所有子图分配给专用变量:

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) fig.suptitle('Sharing x per column, y per row') ax1.plot(x, y) ax2.plot(x, y**2, 'tab:orange') ax3.plot(x, -y, 'tab:green') ax4.plot(x, -y**2, 'tab:red') for ax in fig.get_axes(): ax.label_outer() 共享轴#

默认情况下,每个轴都是单独缩放的。因此,如果范围不同,则子图的刻度值不会对齐。

fig, (ax1, ax2) = plt.subplots(2) fig.suptitle('Axes values are scaled individually by default') ax1.plot(x, y) ax2.plot(x + 1, -y) []

您可以使用sharex或sharey来对齐水平或垂直轴。

fig, (ax1, ax2) = plt.subplots(2, sharex=True) fig.suptitle('Aligning x-axis using sharex') ax1.plot(x, y) ax2.plot(x + 1, -y) []

设置sharex或sharey以True启用在整个网格中的全局共享,即垂直堆叠子图的 y 轴在使用时具有相同的比例sharey=True。

fig, axs = plt.subplots(3, sharex=True, sharey=True) fig.suptitle('Sharing both axes') axs[0].plot(x, y ** 2) axs[1].plot(x, 0.3 * y, 'o') axs[2].plot(x, y, '+') []

对于共享轴的子图,一组刻度标签就足够了。内部轴的刻度标签由sharex和sharey自动删除。子图之间仍然有一个未使用的空白空间。

为了精确控制子图的位置,可以显式地创建一个GridSpecwith Figure.add_gridspec,然后调用它的 subplots方法。例如,我们可以使用 降低垂直子图之间的高度add_gridspec(hspace=0)。

label_outer是一种从不在网格边缘的子图中删除标签和刻度的便捷方法。

fig = plt.figure() gs = fig.add_gridspec(3, hspace=0) axs = gs.subplots(sharex=True, sharey=True) fig.suptitle('Sharing both axes') axs[0].plot(x, y ** 2) axs[1].plot(x, 0.3 * y, 'o') axs[2].plot(x, y, '+') # Hide x labels and tick labels for all but bottom plot. for ax in axs: ax.label_outer()

除了Trueand之外False,sharex和sharey都接受值 'row' 和 'col' 以仅共享每行或每列的值。

fig = plt.figure() gs = fig.add_gridspec(2, 2, hspace=0, wspace=0) (ax1, ax2), (ax3, ax4) = gs.subplots(sharex='col', sharey='row') fig.suptitle('Sharing x per column, y per row') ax1.plot(x, y) ax2.plot(x, y**2, 'tab:orange') ax3.plot(x + 1, -y, 'tab:green') ax4.plot(x + 2, -y**2, 'tab:red') for ax in axs.flat: ax.label_outer()

如果您想要更复杂的共享结构,您可以先创建不共享的轴网格,然后调用axes.Axes.sharex或 axes.Axes.sharey添加共享信息。

fig, axs = plt.subplots(2, 2) axs[0, 0].plot(x, y) axs[0, 0].set_title("main") axs[1, 0].plot(x, y**2) axs[1, 0].set_title("shares x with main") axs[1, 0].sharex(axs[0, 0]) axs[0, 1].plot(x + 1, y + 1) axs[0, 1].set_title("unrelated") axs[1, 1].plot(x + 2, y + 2) axs[1, 1].set_title("also unrelated") fig.tight_layout() 极轴#

参数subplot_kw控制pyplot.subplots子图属性(另请参见Figure.add_subplot)。特别是,这可用于创建极轴网格。

fig, (ax1, ax2) = plt.subplots(1, 2, subplot_kw=dict(projection='polar')) ax1.plot(x, y) ax2.plot(x, y ** 2) plt.show()

脚本总运行时间:(0分7.774秒)

Download Python source code: subplots_demo.py

Download Jupyter notebook: subplots_demo.ipynb

由 Sphinx-Gallery 生成的画廊



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3