Matplotlib 基础:创建折线图

PythonPythonBeginner
立即练习

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

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

简介

Matplotlib 是 Python 中一个流行的数据可视化库。它提供了一个易于使用的接口,用于创建从简单折线图到复杂热图等各种各样的可视化图表。在本实验中,我们将介绍 Matplotlib 的基础知识,并使用“fivethirtyeight”样式表创建一个简单的折线图。

虚拟机使用提示

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/tuples("Tuples") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/ModulesandPackagesGroup -.-> python/standard_libraries("Common Standard Libraries") python/PythonStandardLibraryGroup -.-> python/math_random("Math and Random") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("Numerical Computing") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48741{{"Matplotlib 基础:创建折线图"}} matplotlib/figures_axes -.-> lab-48741{{"Matplotlib 基础:创建折线图"}} matplotlib/line_plots -.-> lab-48741{{"Matplotlib 基础:创建折线图"}} python/tuples -.-> lab-48741{{"Matplotlib 基础:创建折线图"}} python/importing_modules -.-> lab-48741{{"Matplotlib 基础:创建折线图"}} python/standard_libraries -.-> lab-48741{{"Matplotlib 基础:创建折线图"}} python/math_random -.-> lab-48741{{"Matplotlib 基础:创建折线图"}} python/numerical_computing -.-> lab-48741{{"Matplotlib 基础:创建折线图"}} python/data_visualization -.-> lab-48741{{"Matplotlib 基础:创建折线图"}} end

导入 Matplotlib 和 NumPy 库

第一步是导入 Matplotlib 和 NumPy 库。NumPy 是 Python 中用于科学计算的基础包,它提供了强大的数组和线性代数函数。

import matplotlib.pyplot as plt
import numpy as np

将样式设置为“fivethirtyeight”

“fivethirtyeight”样式表复制了广受欢迎的数据驱动新闻网站 FiveThirtyEight.com 的样式。我们将在可视化中使用此样式表。

plt.style.use('fivethirtyeight')

为折线图创建数据

在这一步中,我们将为折线图创建数据。我们将使用 NumPy 的 linspace 函数创建一个在 0 到 10 之间均匀分布的值的数组。我们还将使用 NumPy 的 random.randn 函数生成一些随机噪声。

x = np.linspace(0, 10)
np.random.seed(19680801)
noise = np.random.randn(50)

创建图形和轴对象

接下来,我们将使用 Matplotlib 的 subplots 函数创建一个图形和轴对象。图形对象代表整个图形,轴对象代表图形中的单个子图。

fig, ax = plt.subplots()

绘制数据

在这一步中,我们将使用 Matplotlib 的 plot 函数在轴对象上绘制数据。我们将绘制六条具有不同斜率和随机噪声的不同线条。

ax.plot(x, np.sin(x) + x + noise)
ax.plot(x, np.sin(x) + 0.5 * x + noise)
ax.plot(x, np.sin(x) + 2 * x + noise)
ax.plot(x, np.sin(x) - 0.5 * x + noise)
ax.plot(x, np.sin(x) - 2 * x + noise)
ax.plot(x, np.sin(x) + noise)

设置标题和标签

在这一步中,我们将使用轴对象的 set_titleset_xlabelset_ylabel 方法为图表设置标题和标签。

ax.set_title("'fivethirtyeight' style sheet")
ax.set_xlabel("x")
ax.set_ylabel("y")

显示图表

最后,我们将使用 Matplotlib 的 show 函数来显示图表。

plt.show()

总结

在本实验中,我们学习了如何使用 Matplotlib 中的“fivethirtyeight”样式表创建一个简单的折线图。我们涵盖了创建图形和轴对象、绘制数据以及为图表设置标题和标签的基础知识。掌握这些技能后,你就可以使用 Matplotlib 创建各种各样的可视化图表。