如何在 Python 中獲取有色 3D 曲面?


要透過 Python 獲取有色 3D 曲面,我們可以採取以下步驟 -

步驟

  • 設定圖形大小並調整子圖之間的和周圍填充。

  • 使用 numpy 建立 x 和 y 資料點。

  • 獲取 3D 資料,即 z。

  • 建立一個新的圖形或啟用一個現有的圖形。

  • 獲取 3D 座標軸。

  • 建立一個曲面圖。

  • 要顯示圖形,請使用 show() 方法。

示例

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.linspace(-3, 3, 100)
y = np.cos(x)

x, y = np.meshgrid(x, y)

z = x ** 2 + y ** 2 - 2

fig = plt.figure()

ax = plt.axes(projection='3d')

surf = ax.plot_surface(x, y, z,
cmap=plt.get_cmap('hot'),
edgecolor='none')

plt.show()

輸出

它將產生以下輸出 -

更新時間:2022 年 2 月 2 日

288 次瀏覽

開啟您的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.