问题描述
我有一个3维的numpy数组.我想显示(在matplotlib中)此数组的等值面的漂亮3D图(或更严格地说,显示通过在采样点之间进行插值定义的3D标量字段的等值面).
I have a 3-dimensional numpy array. I'd like to display (in matplotlib) a nice 3D plot of an isosurface of this array (or more strictly, display an isosurface of the 3D scalar field defined by interpolating between the sample points).
matplotlib的mplot3D部分提供了很好的3D绘图支持,但是(据我所知)它的API没有任何东西可以简单地获取标量值的3D数组并显示等值面.但是,它确实支持显示多边形集合,因此大概可以实现行进立方体算法来生成此类多边形.
matplotlib's mplot3D part provides nice 3D plot support, but (so far as I can see) its API doesn't have anything which will simply take a 3D array of scalar values and display an isosurface. However, it does support displaying a collection of polygons, so presumably I could implement the marching cubes algorithm to generate such polygons.
似乎很可能已经在某个地方实现了对科学友好的行进多维数据集,但是我还没有找到它,或者我错过了一些简单的方法来做到这一点.另外,我欢迎任何指向其他工具的指针,这些工具可以可视化从Python/numpy/scipy世界轻松使用的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.
推荐答案
仅需详细说明我的上述评论,matplotlib的3D绘图实际上并不打算用于等值曲面之类的复杂对象.它的目的是为非常简单的3D图产生漂亮的,具有出版质量的矢量输出.它无法处理复杂的3D多边形,因此,即使您自己实施了行进立方体来创建等值面,也无法正确渲染该等值面.
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 来处理和可视化多维数据.
However, what you can do instead is use mayavi (it's mlab API is a bit more convenient than directly using mayavi), which uses VTK to process and visualize multi-dimensional data.
作为一个简单的示例(从mayavi画廊示例之一进行了修改):
As a quick example (modified from one of the mayavi gallery examples):
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()
这篇关于如何在matplotlib mplot3D或类似文件中显示3D阵列等值面的3D图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!