使用 Python、Numpy 和 Matplotlib 繪製蒙版曲面圖
要使用 Python、Numpy 和 Matplotlib 繪製蒙版曲面圖,我們可以採取以下步驟 −
- 設定圖形尺寸並調整子圖之間的和周圍的間距。
- 建立新圖形或啟用現有圖形。
- 將“ax”作為子圖排列的一部分新增到圖形中。
- 從座標向量 pi 和 theta 返回座標矩陣。
- 用蒙版資料點建立 x、y 和 z。
- 用 x、y 和 z 資料點建立曲面圖。
- 要顯示圖形,請使用 show() 方法。
示例
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, projection="3d") pi, theta = np.meshgrid( np.arange(1, 10, 2) * np.pi / 4, np.arange(1, 10, 2) * np.pi / 4) x = np.cos(pi) * np.sin(theta) y = np.sin(pi) * np.sin(theta) z = np.ma.masked_where(x >= 0.01, y) ax.plot_surface(x, y, z, color='red') plt.show()
輸出
它將生成以下輸出
廣告