【问题标题】:Colors on a sphere to depict values球体上的颜色来描述值
【发布时间】:2014-05-07 12:09:58
【问题描述】:

我有一个数据集,对于 theta 和 phi 的离散值,我有一些价值。我想在一个球体上表示它,以便在由极角 theta 和方位角 phi 给出的球体上的点上,颜色显示该特定值。

我如何在 python 中做到这一点?

【问题讨论】:

  • 您是否希望将this 3d examplethis 3d surface 和热图结合起来?
  • @mauve 我不希望表面形状发生变化,我希望它是一个球体,我拥有的值只是极角和方位角的函数,表示领域。对此,我想做一个热图之类的事情。
  • Here 是一个显示设置您自己的颜色映射并将其与值关联的示例,here 是一个 z 值与颜色映射相关联的示例。我希望这会有所帮助。

标签: python matplotlib 3d mayavi


【解决方案1】:

我认为这个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 <gael.varoquaux@normalesup.org>
    # 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 秒。

  • 您可以使用鼠标旋转和缩放图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-11
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    相关资源
    最近更新 更多