3D Contour plot from data using Mayavi / Python

后端 未结 2 404
情歌与酒
情歌与酒 2021-01-31 06:26

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/py

2条回答
  •  一生所求
    2021-01-31 07:13

    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.

    enter image description here

    Take a look at the docs for griddata, note that you can change the interpolation method. If you have more points (both on the interpolated grid, and on the data set), the interpolation gets better and better represents the underlying function you're trying to illustrate. Here is the above example at 10K points and a finer grid:

    enter image description here

提交回复
热议问题