matplotlib从起点出发(12)_Tutorial_12_MultiAxes

在一个Figure中安排多个Axes

通常在一个图像中,需要同时呈现多于一个Axes,并且需要对齐到网格. Matplotlib有多种工具用于处理在本库历史中演变的Axes网格,我们将讨论我们认为用户最常使用的工具,支持Axes组织方式的工具,并提及一些较旧的工具.

注意: Matplotlib使用Axes来引用包含数据、x轴和y轴、刻度、标签、标题等的绘图区域. 有关详细信息,请参阅Parts of a Figure这一节. 另一个经常使用的术语是“subplot”,它指的是与其他Axes对象一起位于Axes风格中的一个Axes.

0 总览

0.1 创建轴的网格形状组合

subplots
用于创建图形和Axes网格的主要功能。它一次创建所有Axes并将其放置在图形上,并返回一个对象数组,其中包含风格中Axes的句柄,参见Figure.subplots.

或者

subplot_mosaic
一种创建图形和Axes风格的简单方法,具有额外的灵活性,即Axes也可以跨越行和列。Axes在带标签的字典中返回,而不是在数组中返回. 另外请参阅Figure.subplot_mosaic以及复杂和语义图形组合(subplot_mosaic).

有时,拥有多组不同的Axes网格是很自然的,在这种情况下,Matplotlib具有SubFigure的概念:

SubFigure
一个虚拟的在一个Figure内的Figure.

0.2 基础工具

这些是GridSpecSubplotSpec的概念:

GridSpec
指定将旋转子图的网格的几何图形. 需要设置网格的行数和列数. 或者,可以调整子图布局参数(例如,左、右等).

SubplotSpec
在给定GridSpec中指定子图的位置.

0.3 一次添加一个Axes

上述函数在单个函数调用中创建所有的Axes,也可以一次添加一个Axes,这最初是Matplotlib的工作方式,这样做通常不太优雅和灵活,但有时对于交互式工作或将Axes旋转在自定义位置很有用.

add_axes
在[left, bottom, width, height]指定的位置添加单个Axes,以图形宽度或高度的小数表示.

subplot或者Figure.add_subplot
在图形上添加一个子图,使用自1开始的索引(继承自Matlab的方式),可以通过指定网格单元格的范围来跨越行和列.

subplot2grid
pyplot.subplot类似,但使用自0开始的索引和二维python切片来选择单元格. 作为手动添加Axes a的简单示例,让我们将3英寸×2英寸的Axes添加到4英寸×3英寸的图形中. 请注意,子图的位置以图形归一化单位定义为[left, bottom, width, height]:

import matplotlib.pyplot as plt
import numpy as np

w, h = 4, 3
margin = 0.5
fig = plt.figure(figsize=(w, h), facecolor='lightblue')
ax = fig.add_axes([margin / w, margin / h, (w - 2 * margin) / w,
                      (h - 2 * margin) / h])

在这里插入图片描述

1 生成网格的高级方法

1.1 基础2×2网格

我们可以使用子图创建一个基本的2×2Axes网格.它返回一个Figure实例和一个Axes对象数组. Axes对象可用于访问将artists放置在Axes上的方法;这里我们使用annotate,但其他示例可能是plot、pcolormesh等.

fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(5.5, 3.5),
                        layout="constrained")
# add an artist, in this case a nice label in the middle...
for row in range(2):
    for col in range(2):
        axs[row, col].annotate(f'axs[{
     row}, {
     col}]', (0.5, 0.5),
                               transform=axs[row, col].transAxes,
                               ha='center', va='center', fontsize=18,
                               color='darkgrey')
fig.suptitle('plt.subplots()')

在这里插入图片描述

我们将对很多Axes进行注释,因此让我们封装annotate, 而不是每次需要时都使用那一大段注释代码:

def annotate_axes(ax, text, fontsize=18):
    ax.text(0.5, 0.5, text, transform=ax.transAxes,
            ha="center", va="center", fontsize=fontsize, color="darkgrey")

使用subplot_mosaic可以实现相同的效果, 但返回类型是字典而不是数组, 用户可以在其中为键提供有用的含义. 这里我们提供了两个列表, 每个列表代表一行, 列表中的每个元素代表列的键.

fig, axd = plt.subplot_mosaic([['upper left', 'upper right'],
                               ['lower left', 'lower right']],
                              figsize=(5.5, 3.5), layout="constrained")
for k, ax in axd.items():
    annotate_axes(ax, f'axd[{
     k!r}]', fontsize=14)
fig.suptitle('plt.subplot_mosaic()')

在这里插入图片描述

1.2 固定纵横比Axes的网格

固定纵横比Axes对于图像或地图很常见. 但是, 它们对布局提出了挑战, 因为对Axes的大小施加了两组约束——它们适合图形, 并且它们具有设定的纵横比. 默认情况下, 这会导致Axes之间的间隙很大:

fig, axs = plt.subplots(2, 2, layout="constrained",
                        figsize=(5.5, 3.5), facecolor='lightblue')
for ax in axs.flat:
    ax.set_aspect(1)
fig.suptitle('Fixed aspect Axes')

在这里插入图片描述

解决此问题的一种方法是将图形的纵横比更改为接近Axes的纵横比, 但这需要反复试验. Matplotlib还提供了 layout=“compressed”, 它将与简单的网格一起使用, 以减少Axes之间的间隙(mpl_toolkits还提供了ImageGrid来实现类似的效果,但使用非标准的Axes类).

fig, axs = plt.subplots(2, 2, layout="compressed", figsize=(5.5, 3.5),
                        facecolor='lightblue')
for ax in axs.flat:
    ax.set_aspect(1)
fig.suptitle('Fixed aspect Axes: compressed')

在这里插入图片描述

1.3 Axes跨越网格中的行或列

有时我们希望Axes跨越网格的行或列. 实际上有多种方法可以实现此目的, 但最方便的方法可能是通过重复其中一个键来使用subplot_mosaic:

fig, axd = plt.subplot_mosaic([['upper left', 'right'],
                               ['lower left', 'right']],
                              figsize=(5.5, 3.5), layout="constrained")
for k, ax in axd.items():
    annotate_axes(ax, f'axd[{
     k!r}]', fontsize=14)
fig.suptitle('plt.subplot_mosaic()')

在这里插入图片描述

有关如何使用GridSpec或subplot2grid执行相同操作的说明, 请参阅下文.

1.4 网格中的可变宽度或高度

子图和subplot_mosaic都允许网格中的行具有不同的高度,并且使用gridspec_kw关键字参数将列设置为不同的宽度. GridSpec接受的间距参数可以传递给子图, 并 subplot_mosaic:

gs_kw = dict(width_ratios=[1.4, 1], height_ratios=[1, 2])
fig, axd = plt.subplot_mosaic([['upper left', 'right'],
                               ['lower left', 'right']],
                              gridspec_kw=gs_kw, figsize=(5.5, 3.5),
                              layout="constrained")
for k, ax in axd.items():
    annotate_axes(ax, f'axd[{
     k!r}]', fontsize=14)
fig.suptitle('plt.subplot_mosaic()')

在这里插入图片描述

1.5 嵌套Axes布局

有时, 拥有两个或多个可能不需要相互关联的Axes网格会很有帮助. 实现此目的的最简单方法是使用Figure.subfigures. 请注意,子图布局是独立的, 因此每个子图中的Axes中心不一定对齐. 请参阅下文, 了解使用 GridSpecFromSubplotSpec实现相同效果的更详细方法.

fig = plt.figure(layout="constrained")
subfigs = fig.subfigures(1, 2, wspace=0.07, width_ratios=[1.5, 1.])
axs0 = subfigs[0].subplots(2, 2)
subfigs[0].set_facecolor('lightblue')
subfigs[0].suptitle('subfigs[0]\nLeft side')
subfigs[0].supxlabel('xlabel for subfigs[0]')

axs1 = subfigs[1].subplots(3, 1)
subfigs[1].suptitle('subfigs[1]')
subfigs[1].supylabel('ylabel for subfigs[1]')

在这里插入图片描述

也可以使用嵌套列表subplot_mosaic嵌套Axes. 此方法不使用子图, 如上所述, 因此无法添加每个子图的suptitle 和 supxlabel等. 相反, 它是下面描述的 subgridspec 方法的方便包装器.

inner = [['innerA'],
         ['innerB']]
outer = [['upper left',  inner],
          ['lower left', 'lower right']]

fig, axd = plt.subplot_mosaic(outer, layout="constrained")
for k, ax in axd.items():
    annotate_axes(ax, f'axd[{
     k!r}]')

在这里插入图片描述

2. 低级和高级的网格方法

在内部, Axes网格的排列是通过创建GridSpec和SubplotSpec的实例来控制的. GridSpec定义了一个(可能是非均匀的)单元格网格. 索引到GridSpec中将返回一个SubplotSpec, 该SubplotSpec涵盖一个或多个网格单元格, 可用于指定Axis的位置.

下面的示例演示如何使用低级方法通过GridSpec对象排列坐标区.

2.1 基础2×2网格

我们可以用与plt.subplots(2,2)相同的方式完成一个2×2网格:

fig = plt.figure(figsize=(5.5, 3.5), layout="constrained")
spec = fig.add_gridspec(ncols=2, nrows=2)

ax0 = fig.add_subplot(spec[0, 0])
annotate_axes(ax0, 'ax0')

ax1 = fig.add_subplot(spec[0, 1])
annotate_axes(ax1, 'ax1')

ax2 = fig.add_subplot(spec[1, 0])
annotate_axes(ax2, 'ax2')

ax3 = fig.add_subplot(spec[1, 1])
annotate_axes(ax3, 'ax3')

fig.suptitle('Manually added subplots using add_gridspec')

在这里插入图片描述

2.2 Axes跨越网格中的行或网格

我们可以使用 Numpy slice语法索引spec数组, 新的Axes将跨越该切片. 这与fig, axd = plt.subplot_mosaic([['ax0', 'ax0'], ['ax1', 'ax2']], ...)效果一样:

fig = plt.figure(figsize=(5.5, 3.5), layout="constrained")
spec = fig.add_gridspec(2, 2)

ax0 = fig.add_subplot(spec[0, :])
annotate_axes(ax0, 'ax0')

ax10 = fig.add_subplot(spec[1, 0])
annotate_axes(ax10, 'ax10')

ax11 = fig.add_subplot(spec[1, 1])
annotate_axes(ax11, 'ax11')

fig.suptitle('Manually added subplots, spanning a column')

在这里插入图片描述

2.3 手动调整GridSpec布局

显式使用GridSpec时, 可以调整从GridSpec创建的子图的布局参数. 请注意, 此选项与受约束的布局或 Figure.tight_layout不兼容, 后者会忽略左侧和右侧, 并调整子图的大小以填充图. 通常, 这种手动放置需要迭代, 以使Axes刻度标签不与Axes重叠.

这些间距参数也可以传递给子图, 并作为gridspec_kw参数subplot_mosaic.

fig = plt.figure(layout=None, facecolor='lightblue')
gs = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.75,
                      hspace=0.1, wspace=0.05)
ax0 = fig.add_subplot(gs[:-1, :])
annotate_axes(ax0, 'ax0')
ax1 = fig.add_subplot(gs[-1, :-1])
annotate_axes(ax1, 'ax1')
ax2 = fig.add_subplot(gs[-1, -1])
annotate_axes(ax2, 'ax2')
fig.suptitle('Manual gridspec with right=0.75')

在这里插入图片描述

2.4 使用SubplotSpec的嵌套布局

你可以使用subgridspec创建类似于子图形的嵌套布局. 在这里, Axes的中心是对齐的.

请注意, 这也可以从更详细的说明获得: gridspec.GridSpecFromSubplotSpec

fig = plt.figure(layout="constrained")
gs0 = fig.add_gridspec(1, 2)

gs00 = gs0[0].subgridspec(2, 2)
gs01 = gs0[1].subgridspec(3, 1)

for a in range(2):
    for b in range(2):
        ax = fig.add_subplot(gs00[a, b])
        annotate_axes(ax, f'axLeft[{
     a}, {
     b}]', fontsize=10)
        if a == 1 and b == 1:
            ax.set_xlabel('xlabel')
for a in range(3):
    ax = fig.add_subplot(gs01[a])
    annotate_axes(ax, f'axRight[{
     a}, {
     b}]')
    if a == 2:
        ax.set_ylabel('ylabel')

fig.suptitle('nested gridspecs')

在这里插入图片描述

下面是一个更复杂的嵌套GridSpec示例: 我们创建一个外部4×4网格, 每个单元格都包含一个内部3×3 Axes 网格. 我们通过在每个内部3×3网格中隐藏适当的脊柱来勾勒外部4×4网格.

def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
    return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d)

fig = plt.figure(figsize=(8, 8), layout='constrained')
outer_grid = fig.add_gridspec(4, 4, wspace=0, hspace=0)

for a in range(4):
    for b in range(4):
        # gridspec inside gridspec
        inner_grid = outer_grid[a, b].subgridspec(3, 3, wspace=0, hspace=0)
        axs = inner_grid.subplots()  # Create all subplots for the inner grid.
        for (c, d), ax in np.ndenumerate(axs):
            ax.plot(*squiggle_xy(a + 1, b + 1, c + 1, d + 1))
            ax.set(xticks=[], yticks=[])

# show only the outside spines
for ax in fig.get_axes():
    ss = ax.get_subplotspec()
    ax.spines.top.set_visible(ss.is_first_row())
    ax.spines.bottom.set_visible(ss.is_last_row())
    ax.spines.left.set_visible(ss.is_first_col())
    ax.spines.right.set_visible(ss.is_last_col())

plt.show()

在这里插入图片描述

3 更多内容

  • 更多细节有关于 subplot mosaic.
  • 更多关于constrained layout的细节, 在大多数示例中用于对齐间距.

参考
以下的函数、方法、类和模块显示在以下例子里:

  • matplotlib.pyplot.subplots
  • matplotlib.pyplot.subplot_mosaic
  • matplotlib.figure.Figure.add_gridspec
  • matplotlib.figure.Figure.add_subplot
  • matplotlib.gridspec.GridSpec
  • matplotlib.gridspec.SubplotSpec.subgridspec
  • matplotlib.gridspec.GridSpecFromSubplotSpec

在这里插入图片描述

相关推荐

  1. <span style='color:red;'>12</span>.<span style='color:red;'>11</span>

    12.11

    2024-01-21 19:56:02      41 阅读
  2. <span style='color:red;'>12</span>.<span style='color:red;'>11</span>

    12.11

    2024-01-21 19:56:02      36 阅读
  3. 12.15

    2024-01-21 19:56:02       47 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-21 19:56:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-21 19:56:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-21 19:56:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-21 19:56:02       18 阅读

热门阅读

  1. Python求解特殊回文数

    2024-01-21 19:56:02       32 阅读
  2. MySQL中的加密函数

    2024-01-21 19:56:02       39 阅读
  3. Docker:容器的两种运行模式(Foreground、Detached)

    2024-01-21 19:56:02       32 阅读
  4. C语言中的递归过程和递归工作栈

    2024-01-21 19:56:02       37 阅读
  5. 计算机网络(第六版)复习提纲6

    2024-01-21 19:56:02       35 阅读
  6. 【笔记】Helm-3 主题-9 Helm高级技术

    2024-01-21 19:56:02       33 阅读
  7. QEMU源码编译CentOS

    2024-01-21 19:56:02       33 阅读