2014-05-07 105 views
2

我有一個數據集,使得對於theta和phi的離散值,我有一些價值。我想在一個球體上表示這一點,使得在由極角θ和方位角φ給出的球體上的點處,顏色顯示出該特定值。描述值的球體上的顏色

我該如何在python中做到這一點?

+0

您是否希望將[此3d示例](http://matplotlib.org/examples/mplot3d/surface3d_demo2.html),[此3d表面](http://matplotlib.org/examples/mplot3d) /trisurf3d_demo.html)和熱圖? – mauve

+0

@mauve我不希望表面形狀發生變化,我希望它是一個球體,我所擁有的值只是極角的函數和表示球體上的點的方位角。在那我想做一個像熱的地圖。 – lovespeed

+0

[這裏](http://matplotlib.org/examples/mplot3d/surface3d_demo3.html)是一個例子,顯示了設置自己的顏色映射並將其與值和[在這裏]關聯(http://matplotlib.org/示例/ mplot3d/surface3d_radial_demo.html)是z值與顏色映射關聯的示例。我希望有所幫助。 – mauve

回答

0

我認爲這spherical harmonics example是你所需要的。

  • 小例子:

    from mayavi import mlab 
    import numpy as np 
    
    # Make sphere, choose colors 
    phi, theta = np.mgrid[0:np.pi:101j, 0:2*np.pi:101j] 
    x, y, z = np.sin(phi) * np.cos(theta), np.sin(phi) * np.sin(theta), np.cos(phi) 
    s = x*y # <-- colors 
    
    # Display 
    mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(600, 500)) 
    mlab.mesh(x, y, z, scalars=s, colormap='Spectral') 
    mlab.view() 
    mlab.show() 
    
  • 您需要python2,不python3。從2015年起
  • 看到這個thread在Ubuntu的14.04我說:

    sudo apt-get install python-vtk python-scipy python-numpy 
    sudo pip install mayavi 
    python main.py # After saving the code below as main.py 
    
  • 下面是完整的代碼:

    # Author: Gael Varoquaux <[email protected]> 
    # Copyright (c) 2008, Enthought, Inc. 
    # License: BSD Style. 
    
    from mayavi import mlab 
    import numpy as np 
    from scipy.special import sph_harm 
    
    # Create a sphere 
    r = 0.3 
    pi = np.pi 
    cos = np.cos 
    sin = np.sin 
    phi, theta = np.mgrid[0:pi:101j, 0:2 * pi:101j] 
    
    x = r * sin(phi) * cos(theta) 
    y = r * sin(phi) * sin(theta) 
    z = r * cos(phi) 
    
    mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(400, 300)) 
    mlab.clf() 
    # Represent spherical harmonics on the surface of the sphere 
    for n in range(1, 6): 
        for m in range(n): 
         s = sph_harm(m, n, theta, phi).real 
    
         mlab.mesh(x - m, y - n, z, scalars=s, colormap='jet') 
    
         s[s < 0] *= 0.97 
    
         s /= s.max() 
         mlab.mesh(s * x - m, s * y - n, s * z + 1.3, 
            scalars=s, colormap='Spectral') 
    
    mlab.view(90, 70, 6.2, (-1.3, -2.9, 0.25)) 
    mlab.show() 
    
  • 可以預計大約20秒的加載時間在這個例子中,如果你的電腦很慢。

  • 您可以使用鼠標旋轉和縮放圖像。