如何在 matplotlib mplot3D 或类似工具中显示 3D 阵列等值面的 3D 图? [英] How to display a 3D plot of a 3D array isosurface in matplotlib mplot3D or similar?

查看:28
本文介绍了如何在 matplotlib mplot3D 或类似工具中显示 3D 阵列等值面的 3D 图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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.

似乎很可能已经在某处实施了一个 scipy 友好的行进立方体,但我还没有找到它,或者我错过了一些简单的方法来做到这一点.或者,我欢迎任何指向其他工具的指针,用于可视化可从 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 图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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