使用 Matplotlib 创建自定义坐标轴网格

Beginner

This tutorial is from open-source community. Access the source code

简介

在本实验中,我们将学习如何使用 mpl_toolkits.axes_grid1 模块,通过 Matplotlib 创建自定义的坐标轴网格。我们将创建两个示例:一个具有固定的坐标轴大小和填充,另一个具有可缩放的坐标轴大小和固定的填充。

虚拟机提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟,以便 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作的验证无法自动化。

如果你在学习过程中遇到问题,请随时向 Labby 提问。课程结束后提供反馈,我们将立即为你解决问题。

导入所需库

我们将首先导入所需的库:用于可视化的 matplotlib.pyplot 和用于创建自定义坐标轴网格的 mpl_toolkits.axes_grid1

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import Divider, Size

定义辅助函数

我们将定义一个辅助函数 label_axes(),用于在坐标轴中心放置一个标签,并移除坐标轴刻度。

def label_axes(ax, text):
    """Place a label at the center of an Axes, and remove the axis ticks."""
    ax.text(.5,.5, text, transform=ax.transAxes,
            horizontalalignment="center", verticalalignment="center")
    ax.tick_params(bottom=False, labelbottom=False,
                   left=False, labelleft=False)

创建具有固定大小和填充的自定义坐标轴网格

我们将创建一个具有固定大小和填充的自定义坐标轴网格。我们将使用 Divider 类将坐标轴矩形划分为一个由 horiz * vert 指定大小的网格。然后,我们将使用 add_axes() 方法向图形中添加四个坐标轴,并使用 Divider 类的 new_locator() 方法指定每个坐标轴的位置。

## Sizes are in inches.
horiz = [Size.Fixed(1.), Size.Fixed(.5), Size.Fixed(1.5), Size.Fixed(.5)]
vert = [Size.Fixed(1.5), Size.Fixed(.5), Size.Fixed(1.)]

rect = (0.1, 0.1, 0.8, 0.8)
fig = plt.figure(figsize=(6, 6))
fig.suptitle("Fixed axes sizes, fixed paddings")

div = Divider(fig, rect, horiz, vert, aspect=False)

## The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")

plt.show()

创建具有可缩放大小和固定填充的自定义坐标轴网格

我们将创建另一个具有可缩放大小和固定填充的自定义坐标轴网格。我们将使用 Size.Scaled() 选项来指定随图形大小缩放的坐标轴大小。其余步骤与上一个示例类似。

## Sizes are in inches.
horiz = [Size.Scaled(1.5), Size.Fixed(.5), Size.Scaled(1.), Size.Scaled(.5)]
vert = [Size.Scaled(1.), Size.Fixed(.5), Size.Scaled(1.5)]

rect = (0.1, 0.1, 0.8, 0.8)
fig = plt.figure(figsize=(6, 6))
fig.suptitle("Scalable axes sizes, fixed paddings")

div = Divider(fig, rect, horiz, vert, aspect=False)

## The rect parameter will actually be ignored and overridden by axes_locator.
ax1 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=0))
label_axes(ax1, "nx=0, ny=0")
ax2 = fig.add_axes(rect, axes_locator=div.new_locator(nx=0, ny=2))
label_axes(ax2, "nx=0, ny=2")
ax3 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, ny=2))
label_axes(ax3, "nx=2, ny=2")
ax4 = fig.add_axes(rect, axes_locator=div.new_locator(nx=2, nx1=4, ny=0))
label_axes(ax4, "nx=2, nx1=4, ny=0")

plt.show()

总结

在本实验中,我们学习了如何使用 mpl_toolkits.axes_grid1 模块通过 Matplotlib 创建自定义坐标轴网格。我们创建了两个示例:一个具有固定的坐标轴大小和填充,另一个具有可缩放的坐标轴大小和固定的填充。我们使用 Divider 类将坐标轴矩形划分为一个由 horiz * vert 指定大小的网格,并使用 Divider 类的 add_axes() 方法和 new_locator() 方法向图形中添加坐标轴。