解釋如何在 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()
輸出
解釋
匯入併為所需的包設定別名。
定義一個使用“正弦”函式生成資料的函式。
使用 NumPy 庫生成 linespace。
呼叫該函式。
定義繪圖並將投影指定為“3d”。
呼叫 Matplotlib 中的“plot_wireframe”函式。
使用“show”函式顯示繪圖。
廣告