用Python绘制和填充3D体积 [英] Plot and fill 3D volumes in Python

查看:319
本文介绍了用Python绘制和填充3D体积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python处理一些3D(体积)数据,对于每个四面体,我不仅拥有顶点的坐标,而且还有第四维,即该四面体体积的某些参数的值.

I am working with some 3D (volumetric) data using Python, and for every tetrahedron, I have not only the vertices's coordinates but also a fourth dimension which is the value of some parameter for that tetrahedron volume.

例如:

# nodes coordinates that defines a tetrahedron volume:
x = [0.0, 1.0, 0.0, 0.0]
y = [0.0, 0.0, 1.0, 0.0]
z = [0.0, 0.0, 0.0, 1.0]
# Scaler value of the potential for the given volume:
c = 100.0

我想绘制一个3D体积(由节点坐标给定),该体积填充有一些纯色,它将表示给定的值C.

I would like to plot a 3D volume (given by the nodes coordinates) filled with some solid color, which would represent the given value C.

我如何在Python 3.6中使用其绘图库来做到这一点?

How could I do that in Python 3.6 using its plotting libraries?

推荐答案

您可以使用mayavi.mlab.triangular_mesh():

from mayavi import mlab

from itertools import combinations, chain

x = [0.0, 1.0, 0.0, 0.0, 2.0, 3.0, 0.0, 0.0]
y = [0.0, 0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 0.0]
z = [0.0, 0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 3.0]
c = [20, 30]

triangles = list(chain.from_iterable(combinations(range(s, s+4), 3) for s in range(0, len(x), 4)))
c = np.repeat(c, 4)

mlab.triangular_mesh(x, y, z, triangles, scalars=c)

这篇关于用Python绘制和填充3D体积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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