使用 Matplotlib 的.step() 和.plot() 函数

MatplotlibMatplotlibBeginner
立即练习

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

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

简介

Matplotlib 是一个用于 Python 编程语言及其数值数学扩展 NumPy 的绘图库。它提供了一个面向对象的 API,用于使用 Tkinter、wxPython、Qt 或 GTK 等通用 GUI 工具包将绘图嵌入到应用程序中。Matplotlib 最初由 John D. Hunter 在 2003 年开发。

本教程将指导你如何在 Matplotlib 中使用 .step().plot() 函数。

虚拟机使用提示

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlotCustomizationGroup(["Plot Customization"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) 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") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/tuples("Tuples") matplotlib/PlotCustomizationGroup -.-> matplotlib/legend_config("Legend Configuration") 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-48960{{"使用 Matplotlib 的.step() 和.plot() 函数"}} matplotlib/figures_axes -.-> lab-48960{{"使用 Matplotlib 的.step() 和.plot() 函数"}} matplotlib/line_plots -.-> lab-48960{{"使用 Matplotlib 的.step() 和.plot() 函数"}} python/tuples -.-> lab-48960{{"使用 Matplotlib 的.step() 和.plot() 函数"}} matplotlib/legend_config -.-> lab-48960{{"使用 Matplotlib 的.step() 和.plot() 函数"}} python/importing_modules -.-> lab-48960{{"使用 Matplotlib 的.step() 和.plot() 函数"}} python/numerical_computing -.-> lab-48960{{"使用 Matplotlib 的.step() 和.plot() 函数"}} python/data_visualization -.-> lab-48960{{"使用 Matplotlib 的.step() 和.plot() 函数"}} end

导入必要的库

首先,我们需要导入必要的库,即 matplotlib.pyplotnumpy

import matplotlib.pyplot as plt
import numpy as np

创建绘图数据

接下来,让我们创建一些用于绘图的数据。我们将使用 numpy.arange() 函数创建一个从 0 到 14 的值数组,并将其存储在变量 x 中。我们还将使用 numpy.sin() 函数创建一个数组,该数组的值是 x 中每个值除以 2 后的正弦值,并将其存储在变量 y 中。

x = np.arange(14)
y = np.sin(x / 2)

使用 .step() 进行绘图

我们可以使用 .step() 函数来创建分段常数曲线。where 参数决定了应该在何处绘制台阶。我们将使用不同的 where 值创建三个绘图。

plt.step(x, y + 2, label='pre (default)', where='pre')
plt.step(x, y + 1, label='mid', where='mid')
plt.step(x, y, label='post', where='post')
plt.legend()
plt.show()

上述代码将创建一个包含三条分段常数曲线的绘图,每条曲线的 where 值都不同。

使用 .plot() 进行绘图

通过使用 .plot() 函数的 drawstyle 参数,我们可以实现与 .step() 相同的效果。我们将使用不同的 drawstyle 值创建三个绘图。

plt.plot(x, y + 2, drawstyle='steps', label='steps (=steps-pre)')
plt.plot(x, y + 1, drawstyle='steps-mid', label='steps-mid')
plt.plot(x, y, drawstyle='steps-post', label='steps-post')
plt.legend()
plt.show()

上述代码将创建一个包含三条分段常数曲线的绘图,每条曲线的 drawstyle 值都不同。

总结

在本教程中,我们学习了如何使用 Matplotlib 中的 .step().plot() 函数来创建分段常数曲线。我们还学习了如何使用 wheredrawstyle 参数来确定应该在何处绘制台阶。