温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - How to display a 3D plot of a 3D array isosurface in matplotlib mplot3D or similar?
matplotlib python scipy visualization volume

python - 如何在matplotlib mplot3D或类似文件中显示3D阵列等值面的3D图?

发布于 2020-03-27 11:27:40

我有一个3维的numpy数组。我想显示(在matplotlib中)此数组的等值面的漂亮3D图(或更严格地说,显示通过在采样点之间进行插值定义的3D标量字段的等值面)。

matplotlib的mplot3D部分提供了不错的3D绘图支持,但是(据我所知),其API没有任何东西可以简单地获取标量值的3D数组并显示等值面。但是,它确实支持显示多边形的集合,因此大概可以实现行进立方体算法来生成此类多边形。

It does seem quite likely that a scipy-friendly marching cubes has already been implemented somewhere and that I haven't found it, or that I'm missing some easy way of doing this. Alternatively I'd welcome any pointers to other tools for visualising 3D array data easily usable from the Python/numpy/scipy world.

查看更多

查看更多

提问者
timday
被浏览
126
150 2019-01-04 03:51

Just to elaborate on my comment above, matplotlib's 3D plotting really isn't intended for something as complex as isosurfaces. It's meant to produce nice, publication-quality vector output for really simple 3D plots. It can't handle complex 3D polygons, so even if implemented marching cubes yourself to create the isosurface, it wouldn't render it properly.

但是,您可以做的是使用mayavi(它的mlab API比直接使用mayavi更为方便),它使用VTK处理和可视化多维数据。

作为一个快速示例(从mayavi画廊示例之一进行了修改):

import numpy as np
from enthought.mayavi import mlab

x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
s = np.sin(x*y*z)/(x*y*z)

src = mlab.pipeline.scalar_field(s)
mlab.pipeline.iso_surface(src, contours=[s.min()+0.1*s.ptp(), ], opacity=0.3)
mlab.pipeline.iso_surface(src, contours=[s.max()-0.1*s.ptp(), ],)

mlab.show()

在此处输入图片说明