复杂且语义化的图形构成 (subplot_mosaic)#

在图中以非均匀网格布局 Axes 可能会很繁琐和冗长。对于密集、均匀的网格,我们有 Figure.subplots,但对于更复杂的布局,例如跨越布局中多列/行的 Axes 或在图中留下空白区域,您可以使用 gridspec.GridSpec (参见 在图中排列多个 Axes) 或手动放置您的 Axes。Figure.subplot_mosaic 旨在提供一个界面,让您可以(以 ASCII 艺术或嵌套列表的形式)直观地布局您的 Axes,以简化此过程。

此接口自然支持为 Axes 命名。Figure.subplot_mosaic 返回一个以用于布局图形的标签为键的字典。通过返回带有名称的数据结构,可以更容易地编写独立于图形布局的绘图代码。

这受到一份 提议的 MEP 和 R 语言的 patchwork 库的启发。虽然我们没有实现运算符重载样式,但我们提供了一个 Pythonic API 来指定(嵌套)Axes 布局。

import matplotlib.pyplot as plt
import numpy as np


# Helper function used for visualization in the following examples
def identify_axes(ax_dict, fontsize=48):
    """
    Helper to identify the Axes in the examples below.

    Draws the label in a large font in the center of the Axes.

    Parameters
    ----------
    ax_dict : dict[str, Axes]
        Mapping between the title / label and the Axes.
    fontsize : int, optional
        How big the label should be.
    """
    kw = dict(ha="center", va="center", fontsize=fontsize, color="darkgrey")
    for k, ax in ax_dict.items():
        ax.text(0.5, 0.5, k, transform=ax.transAxes, **kw)

如果我们想要一个 2x2 网格,我们可以使用 Figure.subplots,它返回一个 2D 的 axes.Axes 数组,我们可以通过索引访问它来绘图。

np.random.seed(19680801)
hist_data = np.random.randn(1_500)


fig = plt.figure(layout="constrained")
ax_array = fig.subplots(2, 2, squeeze=False)

ax_array[0, 0].bar(["a", "b", "c"], [5, 7, 9])
ax_array[0, 1].plot([1, 2, 3])
ax_array[1, 0].hist(hist_data, bins="auto")
ax_array[1, 1].imshow([[1, 2], [2, 1]])

identify_axes(
    {(j, k): a for j, r in enumerate(ax_array) for k, a in enumerate(r)},
)
mosaic

使用 Figure.subplot_mosaic,我们可以生成相同的图形镶嵌,但为 Axes 赋予语义名称

fig = plt.figure(layout="constrained")
ax_dict = fig.subplot_mosaic(
    [
        ["bar", "plot"],
        ["hist", "image"],
    ],
)
ax_dict["bar"].bar(["a", "b", "c"], [5, 7, 9])
ax_dict["plot"].plot([1, 2, 3])
ax_dict["hist"].hist(hist_data)
ax_dict["image"].imshow([[1, 2], [2, 1]])
identify_axes(ax_dict)
mosaic

Figure.subplotsFigure.subplot_mosaic 之间的一个关键区别是返回值。前者返回一个用于索引访问的数组,而后者返回一个将标签映射到所创建的 axes.Axes 实例的字典。

print(ax_dict)
{'bar': <Axes: label='bar'>, 'plot': <Axes: label='plot'>, 'hist': <Axes: label='hist'>, 'image': <Axes: label='image'>}

字符串速记#

通过将 Axes 标签限制为单个字符,我们可以将我们想要的 Axes “绘制”成 “ASCII 艺术”。如下所示

mosaic = """
    AB
    CD
    """

将为我们提供一个 2x2 网格中布局的 4 个 Axes,并生成与上面相同的图形镶嵌(但现在标签是 {"A", "B", "C", "D"} 而不是 {"bar", "plot", "hist", "image"})。

fig = plt.figure(layout="constrained")
ax_dict = fig.subplot_mosaic(mosaic)
identify_axes(ax_dict)
mosaic

或者,您可以使用更紧凑的字符串表示法

mosaic = "AB;CD"

将为您提供相同的构成,其中 ";" 被用作行分隔符而不是换行符。

fig = plt.figure(layout="constrained")
ax_dict = fig.subplot_mosaic(mosaic)
identify_axes(ax_dict)
mosaic

跨越多行/列的 Axes#

我们可以使用 Figure.subplot_mosaic 来做一些 Figure.subplots 无法做到的事情,那就是指定一个 Axes 应该跨越多行或多列。

如果我们想重新排列我们的四个 Axes,使 "C" 在底部水平跨越,并且 "D" 在右侧垂直跨越,我们可以这样做

axd = plt.figure(layout="constrained").subplot_mosaic(
    """
    ABD
    CCD
    """
)
identify_axes(axd)
mosaic

如果我们不想用 Axes 填充图中所有的空间,我们可以指定网格中的一些空间留空

axd = plt.figure(layout="constrained").subplot_mosaic(
    """
    A.C
    BBB
    .D.
    """
)
identify_axes(axd)
mosaic

如果我们更喜欢使用另一个字符(而不是句点 ".")来标记空位,我们可以使用 empty_sentinel 来指定要使用的字符。

axd = plt.figure(layout="constrained").subplot_mosaic(
    """
    aX
    Xb
    """,
    empty_sentinel="X",
)
identify_axes(axd)
mosaic

在内部,我们使用的字母没有特殊含义,任何 Unicode 码点都是有效的!

axd = plt.figure(layout="constrained").subplot_mosaic(
    """αб
       ℝ☢"""
)
identify_axes(axd)
mosaic

不建议在字符串速记中将空格用作标签或空占位符,因为它在处理输入时可能会被去除。

控制镶嵌创建#

此功能是基于 gridspec 构建的,您可以将关键字参数传递给底层的 gridspec.GridSpec (与 Figure.subplots 相同)。

在这种情况下,我们希望使用输入来指定排列,但设置行/列的相对宽度。为方便起见,gridspec.GridSpecheight_ratioswidth_ratiosFigure.subplot_mosaic 调用序列中暴露出来。

axd = plt.figure(layout="constrained").subplot_mosaic(
    """
    .a.
    bAc
    .d.
    """,
    # set the height ratios between the rows
    height_ratios=[1, 3.5, 1],
    # set the width ratios between the columns
    width_ratios=[1, 3.5, 1],
)
identify_axes(axd)
mosaic

其他 gridspec.GridSpec 关键字可以通过 gridspec_kw 传递。例如,使用 {left, right, bottom, top} 关键字参数来定位整个镶嵌,以便在图中放置同一镶嵌的多个版本。

mosaic = """AA
            BC"""
fig = plt.figure()
axd = fig.subplot_mosaic(
    mosaic,
    gridspec_kw={
        "bottom": 0.25,
        "top": 0.95,
        "left": 0.1,
        "right": 0.5,
        "wspace": 0.5,
        "hspace": 0.5,
    },
)
identify_axes(axd)

axd = fig.subplot_mosaic(
    mosaic,
    gridspec_kw={
        "bottom": 0.05,
        "top": 0.75,
        "left": 0.6,
        "right": 0.95,
        "wspace": 0.5,
        "hspace": 0.5,
    },
)
identify_axes(axd)
mosaic

或者,您可以使用子图功能

mosaic = """AA
            BC"""
fig = plt.figure(layout="constrained")
left, right = fig.subfigures(nrows=1, ncols=2)
axd = left.subplot_mosaic(mosaic)
identify_axes(axd)

axd = right.subplot_mosaic(mosaic)
identify_axes(axd)
mosaic

控制子图创建#

我们还可以传递用于创建子图的参数(同样,与 Figure.subplots 相同),这些参数将应用于所有创建的 Axes。

axd = plt.figure(layout="constrained").subplot_mosaic(
    "AB", subplot_kw={"projection": "polar"}
)
identify_axes(axd)
mosaic

每个 Axes 的子图关键字参数#

如果需要单独控制传递给每个子图的参数,请使用 per_subplot_kw 来传递 Axes 标识符(或 Axes 标识符元组)与要传递的关键字字典之间的映射。

版本 3.7 新增。

fig, axd = plt.subplot_mosaic(
    "AB;CD",
    per_subplot_kw={
        "A": {"projection": "polar"},
        ("C", "D"): {"xscale": "log"}
    },
)
identify_axes(axd)
mosaic

如果布局是用字符串速记指定的,那么我们知道 Axes 标签将是单个字符,并且可以明确地解释 per_subplot_kw 中的更长字符串,以指定要应用关键字的一组 Axes。

fig, axd = plt.subplot_mosaic(
    "AB;CD",
    per_subplot_kw={
        "AD": {"projection": "polar"},
        "BC": {"facecolor": ".9"}
    },
)
identify_axes(axd)
mosaic

如果 subplot_kwper_subplot_kw 一起使用,则它们会被合并,其中 per_subplot_kw 具有优先权。

axd = plt.figure(layout="constrained").subplot_mosaic(
    "AB;CD",
    subplot_kw={"facecolor": "xkcd:tangerine"},
    per_subplot_kw={
        "B": {"facecolor": "xkcd:water blue"},
        "D": {"projection": "polar", "facecolor": "w"},
    }
)
identify_axes(axd)
mosaic

嵌套列表输入#

我们用字符串速记可以做的一切,在传入列表时也可以做到(内部我们会将字符串速记转换为嵌套列表),例如使用跨度、空白和 gridspec_kw

axd = plt.figure(layout="constrained").subplot_mosaic(
    [
        ["main", "zoom"],
        ["main", "BLANK"],
    ],
    empty_sentinel="BLANK",
    width_ratios=[2, 1],
)
identify_axes(axd)
mosaic

此外,使用列表输入,我们可以指定嵌套镶嵌。内部列表的任何元素都可以是另一组嵌套列表。

inner = [
    ["inner A"],
    ["inner B"],
]

outer_nested_mosaic = [
    ["main", inner],
    ["bottom", "bottom"],
]
axd = plt.figure(layout="constrained").subplot_mosaic(
    outer_nested_mosaic, empty_sentinel=None
)
identify_axes(axd, fontsize=36)
mosaic

我们还可以传入一个 2D NumPy 数组来做一些事情,例如

mosaic = np.zeros((4, 4), dtype=int)
for j in range(4):
    mosaic[j, j] = j + 1
axd = plt.figure(layout="constrained").subplot_mosaic(
    mosaic,
    empty_sentinel=0,
)
identify_axes(axd)
mosaic

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

由 Sphinx-Gallery 生成的画廊