首頁  >  文章  >  後端開發  >  以下是一些適合文章內容的基於問題的標題: * 如何使用 Matplotlib 的 `plot_surface` 函數從 3D 點集合中繪製曲面? * 在 Matplo 中繪製曲面

以下是一些適合文章內容的基於問題的標題: * 如何使用 Matplotlib 的 `plot_surface` 函數從 3D 點集合中繪製曲面? * 在 Matplo 中繪製曲面

Barbara Streisand
Barbara Streisand原創
2024-10-26 18:33:02914瀏覽

Here are a few question-based titles that fit the article content:

* How can I plot a surface from a collection of 3D points using Matplotlib's `plot_surface` function?
* Plotting Surfaces in Matplotlib: How do I prepare 3D point data for `plot_surface`

在 Matplotlib 中繪製曲面

當處理表示為 3 元組的 3D 點集合時,無法立即清楚plot_surface 函數是否是理想的選擇曲面繪圖。讓我們深入研究細微差別,以了解如何為此函數準備資料。

理解plot_surface

plot_surface 需要 X、Y 和 Z 為二維陣列。與函數 f(x, y) -> 的曲面圖不同z,您可以在其中提供網格域,點雲提出了挑戰,因為它需要三角測量。

資料轉換

由於您的資料是 3D 點的列表,因此您需要將其轉換為plot_surface可以理解的格式。一種方法是使用 meshgrid 建立網格域,如以下程式碼片段所示:

<code class="python">import numpy as np

data = [(x1,y1,z1),(x2,y2,z2),.....,(xn,yn,zn)]
x, y = zip(*data)  # Extract x and y coordinates from the tuples
X, Y = np.meshgrid(x, y)  # Create a grid domain for X and Y

# Convert the z coordinates into a 2D array
zs = np.array([z for x, y, z in data]).reshape(X.shape)</code>

現在,您已擁有所需的 2D 陣列格式的 X、Y 和 Z。

曲面繪圖

準備好資料後,您可以使用plot_surface繼續繪製曲面:

<code class="python">from mpl_toolkits.mplot3d import Axes3D  # Enable 3D plotting
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')  # Create a 3D subplot

ax.plot_surface(X, Y, Z)</code>

這應該會產生穿過給定點的平滑曲面。

以上是以下是一些適合文章內容的基於問題的標題: * 如何使用 Matplotlib 的 `plot_surface` 函數從 3D 點集合中繪製曲面? * 在 Matplo 中繪製曲面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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