Matplotlib 色条内嵌轴

PythonPythonBeginner
立即练习

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

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

本教程将向你展示如何使用 mpl_toolkits.axes_grid1.inset_locator.inset_axes 来控制色条的位置、高度和宽度。当你想要在绘图中添加色条但又想自定义其大小和位置时,这会很有用。

虚拟机使用提示

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") python/BasicConceptsGroup -.-> python/comments("Comments") matplotlib/PlottingDataGroup -.-> matplotlib/heatmaps("Heatmaps") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/ModulesandPackagesGroup -.-> python/using_packages("Using Packages") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48670{{"Matplotlib 色条内嵌轴"}} matplotlib/figures_axes -.-> lab-48670{{"Matplotlib 色条内嵌轴"}} python/comments -.-> lab-48670{{"Matplotlib 色条内嵌轴"}} matplotlib/heatmaps -.-> lab-48670{{"Matplotlib 色条内嵌轴"}} python/lists -.-> lab-48670{{"Matplotlib 色条内嵌轴"}} python/tuples -.-> lab-48670{{"Matplotlib 色条内嵌轴"}} python/importing_modules -.-> lab-48670{{"Matplotlib 色条内嵌轴"}} python/using_packages -.-> lab-48670{{"Matplotlib 色条内嵌轴"}} python/data_visualization -.-> lab-48670{{"Matplotlib 色条内嵌轴"}} end

导入所需库

首先,我们需要导入所需的库:matplotlib 以及 mpl_toolkits.axes_grid1 中的 inset_axes。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

创建绘图和图像

接下来,我们将创建一个绘图和一幅图像,以展示如何使用内嵌轴添加色条。

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[6, 3])

im1 = ax1.imshow([[1, 2], [2, 3]])
im2 = ax2.imshow([[1, 2], [2, 3]])

使用内嵌轴添加色条

现在,我们将使用 inset_axes 为每幅图像添加一个色条。第一个色条将添加到 ax1,第二个添加到 ax2

## 为ax1添加色条
axins1 = inset_axes(
    ax1,
    width="50%",  ## 宽度:父边界框宽度的50%
    height="5%",  ## 高度:5%
    loc="upper right",
)
axins1.xaxis.set_ticks_position("bottom")
fig.colorbar(im1, cax=axins1, orientation="horizontal", ticks=[1, 2, 3])

## 为ax2添加色条
axins2 = inset_axes(
    ax2,
    width="5%",  ## 宽度:父边界框宽度的5%
    height="50%",  ## 高度:50%
    loc="lower left",
    bbox_to_anchor=(1.05, 0., 1, 1),
    bbox_transform=ax2.transAxes,
    borderpad=0,
)
fig.colorbar(im2, cax=axins2, ticks=[1, 2, 3])

显示绘图

最后,我们将使用 plt.show() 显示绘图。

plt.show()

总结

在本教程中,我们学习了如何使用 mpl_toolkits.axes_grid1.inset_locator.inset_axes 为具有自定义大小和位置的绘图添加色条。inset_axes 函数使我们能够控制色条的位置、高度和宽度,从而更轻松地创建出专业外观的绘图。