Matplotlib像Matlab的trisurf [英] Matplotlib like matlab's trisurf

查看:549
本文介绍了Matplotlib像Matlab的trisurf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To make a long story short, I'd like to plot a generic 3D triangle mesh in python. Matplotlib seems to be the ideal candidate, but I'd go with any 3D rendering that can do what I'm about to describe.

假设我有一个由X,Y和Z定义的三角形网格,一个点云的3D坐标(每个向量的长度为n)和UVW(一个二维mx-3矩阵),其中每行是一个三元组索引到点云.这个三元组代表一个单独的三角形.换句话说,我在n个点上有m个三角形.在Matlab中,我要做的是生成3D图:

Suppose I have a triangle mesh defined by X, Y, and Z, the 3D coordinates of a point cloud, each a vector of length n, and UVW, a 2D m-x-3 matrix in which each row is a triplet of indices into the point cloud. This triplet represents an individual triangle. In other words, I have m triangles over n points. In Matlab, to generated a 3D plot, I just do:

trisurf(UVW, X, Y, Z)

有人对此有任何经验吗?特别是,mpsurs trisurf会被折磨吗?

Does anyone have any experience with this? In particular, can mplots trisurf be shoehorned to work?

推荐答案

根据戴维斯的评论,根据您的性能需求,mayavi最适合此操作.

Depending on your performance needs, mayavi is likely to be best suited for this - as per Davis comment.

但是,matplotlib随附您可以按照您的描述完美地将通用UVWXYZ传递给plot_trisurf .

However, matplotlib comes with plot_trisurf to which you can perfectly pass generic UVW, X, Y , Z as you describe.

带有环网的示例:

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()

这篇关于Matplotlib像Matlab的trisurf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆