解释如何在 Python 中使用 Matplotlib 创建线框图?
Matplotlib 是一个流行的 Python 数据可视化包。数据可视化是一个重要步骤,因为它有助于理解数据中的情况,而无需查看数字并执行复杂的计算。它有助于有效地向受众传达定量见解。
Matplotlib 用于使用数据创建二维图。它带有一个面向对象的 API,有助于将图嵌入 Python 应用程序中。Matplotlib 可与 IPython shell、Jupyter notebook、Spyder IDE 等一起使用。它是用 Python 编写的。它使用 NumPy 创建,NumPy 是 Python 中的数值 Python 包。
可以使用以下命令在 Windows 上安装 Python:
pip install matplotlib
Matplotlib 的依赖项为:
Python ( greater than or equal to version 3.4) NumPy Setuptools Pyparsing Libpng Pytz Free type Six Cycler Dateutil
让我们了解如何使用 Matplotlib 创建线框图:
示例
from mpl_toolkits import mplot3d import numpy as np import matplotlib.pyplot as plt def my_fun(x, y): return np.sin(np.sqrt(x ** 4 + y ** 4)) x = np.linspace(−8, 8, 30) y = np.linspace(−8, 8, 30) X, Y = np.meshgrid(x, y) Z = my_fun(X, Y) fig = plt.figure() ax = plt.axes(projection='3d') ax.plot_wireframe(X, Y, Z, color='red') plt.xlabel('X axis') plt.ylabel('Y axis') ax.set_title('A wireframe plot') plt.show()
输出
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
解释
导入并为所需的包设置别名。
定义一个使用“正弦”函数生成数据的函数。
使用 NumPy 库生成 linespace。
调用该函数。
定义绘图并将投影指定为“3d”。
调用 Matplotlib 中的“plot_wireframe”函数。
使用“show”函数显示绘图。
广告