gpt4 book ai didi

python - Matplotlib 类似于 matlab trisurf

转载 作者:行者123 更新时间:2023-11-28 20:22:14 25 4
gpt4 key购买 nike

长话短说,我想在 python 中绘制一个通用的 3D 三角形网格。 Matplotlib 似乎是理想的候选者,但我会选择任何可以完成我将要描述的内容的 3D 渲染。

假设我有一个由 X、Y 和 Z 定义的三角形网格,点云的 3D 坐标,每个坐标都是长度为 n 的向量,以及 UVW,一个 2D m-x-3 矩阵,其中每一行都是三元组点云中的索引。这个三元组代表一个单独的三角形。换句话说,我在 n 个点上有 m 个三角形。在 Matlab 中,要生成 3D 图,我只需执行以下操作:

trisurf(UVW, X, Y, Z)

有没有人有这方面的经验?特别是,mplots trisurf 是否可以硬塞来工作?

最佳答案

根据您的性能需求,mayavi 可能最适合此 - 根据戴维斯的评论。

然而,matplotlib 自带 plot_trisurf您可以按照您的描述完美地将通用 UVWXYZ 传递给它。

环面网格示例:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.tri as mtri

R = 1.
r = 0.8
n = 50
m = 50

def torus_triangles(n, m):
""" Returns triangles to mesh a (n, m) torus """
tri = []
for i in range(n):
for j in range(m):
a = i + j*(n)
b = ((i+1) % n) + j*n
d = i + ((j+1) % m) * n
c = ((i+1) % n) + ((j+1) % m) * n
tri += [[a, b, d], [b, c, d]]
return np.array(tri, dtype=np.int32)

theta0 = np.linspace(0, (2*np.pi), n, endpoint=False)
phi0 = np.linspace(0, (2*np.pi), m, endpoint=False)
theta, phi = np.meshgrid(theta0, phi0)

x = (R + r * np.sin(phi)) * np.cos(theta)
y = (R + r * np.sin(phi)) * np.sin(theta)
z = r * np.cos(phi)

triangles = torus_triangles(n , m)
triang = mtri.Triangulation(x.ravel(), y.ravel(), triangles)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(triang, z.ravel(), lw=0.2, edgecolor="black", color="grey",
alpha=0.5)

plt.show()

enter image description here

关于python - Matplotlib 类似于 matlab trisurf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24560309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com