创建相邻子图

PythonPythonBeginner
立即练习

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

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

简介

在数据可视化中,创建共享公共轴的多个图表通常很有用。这可以使用Matplotlib中的 subplots 函数来实现。在本教程中,我们将学习如何创建共享公共x轴的相邻子图。

虚拟机使用提示

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL 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"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") python/BasicConceptsGroup -.-> python/booleans("Booleans") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("Numerical Computing") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48751{{"创建相邻子图"}} matplotlib/figures_axes -.-> lab-48751{{"创建相邻子图"}} python/booleans -.-> lab-48751{{"创建相邻子图"}} matplotlib/line_plots -.-> lab-48751{{"创建相邻子图"}} python/lists -.-> lab-48751{{"创建相邻子图"}} python/tuples -.-> lab-48751{{"创建相邻子图"}} python/importing_modules -.-> lab-48751{{"创建相邻子图"}} python/numerical_computing -.-> lab-48751{{"创建相邻子图"}} python/data_visualization -.-> lab-48751{{"创建相邻子图"}} end

导入库

我们首先导入必要的库——numpymatplotlib.pyplot

import numpy as np
import matplotlib.pyplot as plt

生成数据

我们生成一些要绘制的示例数据。在这里,我们使用 numpy 库生成三个数据数组。

t = np.arange(0.0, 2.0, 0.01)

s1 = np.sin(2 * np.pi * t)
s2 = np.exp(-t)
s3 = s1 * s2

创建子图

我们使用Matplotlib中的 subplots 函数创建三个子图。我们将 sharex 参数设置为 True,以确保子图共享一个公共x轴。我们还使用 subplots_adjust 函数去除子图之间的垂直间距。

fig, axs = plt.subplots(3, 1, sharex=True)
fig.subplots_adjust(hspace=0)

绘制数据

我们在每个子图上绘制数据,并为每个图设置y轴刻度值和范围。

axs[0].plot(t, s1)
axs[0].set_yticks(np.arange(-0.9, 1.0, 0.4))
axs[0].set_ylim(-1, 1)

axs[1].plot(t, s2)
axs[1].set_yticks(np.arange(0.1, 1.0, 0.2))
axs[1].set_ylim(0, 1)

axs[2].plot(t, s3)
axs[2].set_yticks(np.arange(-0.9, 1.0, 0.4))
axs[2].set_ylim(-1, 1)

显示图表

我们使用Matplotlib中的 show 函数来显示图表。

plt.show()

总结

在本教程中,我们学习了如何使用Matplotlib中的 subplots 函数创建共享公共x轴的相邻子图。我们还学习了如何为每个图设置y轴刻度值和范围。这种技术在数据可视化中很有用,可用于比较共享同一轴的多个数据集。