使用Mayavi/Python从数据中提取3D轮廓图 [英] 3D Contour plot from data using Mayavi / Python
问题描述
我想使用Mayavi进行3D等高线图绘制,其方式与本页第三幅图(氢电子云模型)完全相同:
I would like to do a 3D contour plot using Mayavi in exactly the same way as the third figure on this page (a hydrogen electron cloud model) :
http://www.sethanil.com/python-for-reseach/5
我有一组数据点,这些数据点是使用我想使用的自己的模型创建的.数据点存储在多维numpy数组中,如下所示:
I have a set of data points which I created using my own model which I would like to use. The data points are stored in a multi-dimensional numpy array like so:
XYZV = [[1, 2, 3, 4],
[6, 7, 8, 9],
...
[4, 5, 6, 7]]
数据点在XYZ空间中分布不均匀,并且没有以任何特定顺序存储.我认为该示例使用了网格网格来生成数据点-我已经看过了,但完全不理解.任何帮助将不胜感激?
The data points are not uniformly spread in XYZ space and not stored in any particular order. I think the example uses a meshgrid to generate the data points - I have looked this up but totally don't understand it. Any help would be much appreciated?
(来源: sethanil. com )
(source: sethanil.com)
推荐答案
诀窍是在绘制之前在网格上进行插值-为此,我将使用scipy
.在R
下面是一个(500,3)XYZ值数组,而V
是每个XYZ点处的幅值".
The trick is to interpolate over a grid before you plot - I'd use scipy
for this. Below R
is a (500,3) array of XYZ values and V
is the "magnitude" at each XYZ point.
from scipy.interpolate import griddata
import numpy as np
# Create some test data, 3D gaussian, 200 points
dx, pts = 2, 100j
N = 500
R = np.random.random((N,3))*2*dx - dx
V = np.exp(-( (R**2).sum(axis=1)) )
# Create the grid to interpolate on
X,Y,Z = np.mgrid[-dx:dx:pts, -dx:dx:pts, -dx:dx:pts]
# Interpolate the data
F = griddata(R, V, (X,Y,Z))
从这里可以轻松显示我们的数据:
From here it's a snap to display our data:
from mayavi.mlab import *
contour3d(F,contours=8,opacity=.2 )
这给出了一个很好的(块状)高斯.
This gives a nice (lumpy) Gaussian.
看看 griddata 的文档
这篇关于使用Mayavi/Python从数据中提取3D轮廓图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!