首頁  >  文章  >  後端開發  >  如何在 Matplotlib 中從 3D 點列表繪製曲面?

如何在 Matplotlib 中從 3D 點列表繪製曲面?

DDD
DDD原創
2024-10-26 06:37:03480瀏覽

How to Plot a Surface from a List of 3D Points in Matplotlib?

Matplotlib 中的曲面繪圖

處理空間資料時,生成曲面可以提供有價值的見解。在 Matplotlib 中,plot_surface 函數用於此目的。但是,它需要 2D 數組形式的輸入,這使得它與表示 3D 點的 3 元組列表不相容。

資料轉換

繪製曲面從三元組中,我們需要將資料轉換為所需的格式。由於給定的資料缺乏任何底層函數 f(x, y),因此我們必須對點進行三角剖分以創建曲面。

三角剖分與變換

對於三角剖分過程,我們可以使用[triangulate_points](https://matplotlib.org/stable/gallery/mplot3d/ triangulation_demo.html) 函數。它產生連接所提供的點的三角形列表,從而有效地創建表面網格。

三角測量後,我們可以得到 X、Y 和 Z 所需的 2D 陣列。這些陣列表示頂點的座標

繪製表面

使用轉換後的數據,我們現在可以呼叫plot_surface:

<code class="python">import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

# List of 3-tuples representing points
data = [(x1, y1, z1), (x2, y2, z2), ...]

# Triangulate the points
triangles = triangulate_points(data)

# Extract 2D arrays for X, Y, and Z
X, Y, Z = zip(*triangles)

# Plot the surface
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()</code>

此腳本將產生一個覆蓋給定3D 點的光滑表面。

以上是如何在 Matplotlib 中從 3D 點列表繪製曲面?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn