如何使用 matplotlib 中的元組列表繪製 3D 表面?


要使用 matplotlib 中的元組列表繪製 3D 表面,我們可以執行以下步驟。

步驟

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

  • 生成元組列表。

  • 從元組列表中獲取 x、yz 資料點。

  • 從座標向量返回座標矩陣。

  • 獲取用於繪製曲面的 h 資料點。

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

  • 獲取圖形的當前座標系 3d。

  • 建立一個曲面圖。

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

示例

import numpy as np
from matplotlib import pyplot as plt

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

# List of tuples
data = [(1, 3, 2), (3, 5, 2), (4, 7, 4), (8, 7, 4),
        (3, 6, 1), (3, 9, 0), (3, 9, 0)]

# Data points from the list of tuples
x, y, z = zip(*data)

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

h = x ** 2 + y ** 2

fig = plt.figure()

# Get the current axis
ax = fig.gca(projection='3d')

# Surface plot
ax.plot_surface(x, y, h, cmap='plasma')

plt.show()

輸出

將生成以下輸出 -

更新於: 2021 年 10 月 8 日

2K+ 瀏覽量

開啟你的事業

完成課程後獲得認證

開始
廣告
© . All rights reserved.