面向 Python 初学者的 Matplotlib 绘图

MatplotlibMatplotlibBeginner
立即练习

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

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

简介

Matplotlib 是一个用于 Python 编程语言及其数值数学扩展 NumPy 的绘图库。它提供了一个面向对象的 API,用于使用 Tkinter、wxPython、Qt 或 GTK 等通用 GUI 工具包将绘图嵌入应用程序中。它还为非交互式绘图提供了一个过程式接口。

在这个实验中,你将学习如何使用 Matplotlib 创建一个简单的绘图。

虚拟机使用提示

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlotCustomizationGroup(["Plot Customization"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") matplotlib/BasicConceptsGroup -.-> matplotlib/saving_figures("Saving Figures to File") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") matplotlib/PlotCustomizationGroup -.-> matplotlib/titles_labels("Adding Titles and Labels") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48887{{"面向 Python 初学者的 Matplotlib 绘图"}} matplotlib/figures_axes -.-> lab-48887{{"面向 Python 初学者的 Matplotlib 绘图"}} matplotlib/saving_figures -.-> lab-48887{{"面向 Python 初学者的 Matplotlib 绘图"}} matplotlib/line_plots -.-> lab-48887{{"面向 Python 初学者的 Matplotlib 绘图"}} python/lists -.-> lab-48887{{"面向 Python 初学者的 Matplotlib 绘图"}} python/tuples -.-> lab-48887{{"面向 Python 初学者的 Matplotlib 绘图"}} matplotlib/titles_labels -.-> lab-48887{{"面向 Python 初学者的 Matplotlib 绘图"}} python/importing_modules -.-> lab-48887{{"面向 Python 初学者的 Matplotlib 绘图"}} python/data_visualization -.-> lab-48887{{"面向 Python 初学者的 Matplotlib 绘图"}} end

导入Matplotlib库

要在Python中使用Matplotlib,你需要先导入它。输入以下代码来导入Matplotlib库:

import matplotlib.pyplot as plt

创建一个简单的绘图

要在Matplotlib中创建一个简单的绘图,你需要提供一个你想要绘制的数字列表。在这种情况下,我们将绘制一个数字列表与其索引的关系图,结果将是一条直线。使用格式字符串(这里是'o-r')来设置标记(圆圈)、线条样式(实线)和颜色(红色)。

plt.plot([1, 2, 3, 4], 'o-r')
plt.ylabel('some numbers')
plt.show()

自定义绘图

Matplotlib提供了许多选项来自定义绘图。你可以更改颜色、线条样式、标记样式以及许多其他选项。以下是一个如何将线条颜色更改为蓝色并将标记样式更改为加号的示例:

plt.plot([1, 2, 3, 4], '+-b')
plt.ylabel('some numbers')
plt.show()

添加标签和标题

为绘图添加标签和标题对于使其更具信息性至关重要。以下代码为绘图添加了一个标题以及x轴和y轴的标签:

plt.plot([1, 2, 3, 4], 'o-r')
plt.title('Simple Plot')
plt.xlabel('Index')
plt.ylabel('Numbers')
plt.show()

保存绘图

你可以使用 savefig 方法将绘图保存为图像文件。以下代码将绘图保存为PNG图像:

plt.plot([1, 2, 3, 4], 'o-r')
plt.title('Simple Plot')
plt.xlabel('Index')
plt.ylabel('Numbers')
plt.savefig('simple_plot.png')

总结

在本实验中,你已经学习了如何使用Matplotlib创建一个简单的绘图。你还学习了如何自定义绘图、添加标签和标题以及将绘图保存为图像文件。Matplotlib提供了许多选项来创建信息丰富且视觉上吸引人的绘图。