【问题标题】:How do I create a colormap plot that can make a sphere in matplotlib 3d look like half i bright (look like the earth is illuminated on one side)如何创建可以使 matplotlib 3d 中的球体看起来像一半亮的颜色图图(看起来像地球的一侧被照亮)
【发布时间】:2019-10-28 15:44:27
【问题描述】:

我正在尝试在 matplotlib 中创建一个 3D 球体,并让它的颜色就像球体的一侧被太阳照亮一样。

我尝试过使用 matplotlib 颜色图。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_axis_off()
phi = np.linspace(0,2*np.pi, 50)
theta = np.linspace(0, np.pi, 25)
x=np.outer(np.cos(phi), np.sin(theta))
y=np.outer(np.sin(phi), np.sin(theta))
z=np.outer(np.ones(np.size(phi)), np.cos(theta))
PHI=np.outer(phi,np.ones(np.size(theta)))
THETA=np.outer(np.ones(np.size(phi)),theta)
data = PHI/np.pi
norm = plt.Normalize(vmin=data.min(), vmax=data.max()) 
surface=ax.plot_surface(x, y, z, cstride=1, rstride=1, 
                        facecolors=cm.jet(norm(data)))
surface=ax.plot_surface(x, y, z, cstride=1, rstride=1, 
                        facecolors=cm.binary(norm(data)),cmap=plt.get_cmap('jet'))
plt.show()

我期待一个看起来像这样的球体: Or basically something that looks like the earth with the day side and the night side

但我的结果是这样的: current plot from the above code

【问题讨论】:

    标签: python numpy matplotlib plot 3d


    【解决方案1】:

    你需要使用LightSource包:

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib.colors import LightSource
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.set_axis_off()
    phi = np.linspace(0,2*np.pi, 100)
    theta = np.linspace(0, np.pi, 50)
    x=np.outer(np.cos(phi), np.sin(theta))
    y=np.outer(np.sin(phi), np.sin(theta))
    z=np.outer(np.ones(np.size(phi)), np.cos(theta))
    PHI=np.outer(phi,np.ones(np.size(theta)))
    THETA=np.outer(np.ones(np.size(phi)),theta)
    data = PHI/np.pi
    norm = plt.Normalize(vmin=data.min(), vmax=data.max())
    
    
    # use Light Source
    ls = LightSource(0, 0)
    # create rgb shade
    rgb = ls.shade(x, cmap=cm.Wistia, vert_exag=0.1, blend_mode='soft')
    # blend shade
    bsl = ls.blend_hsv(rgb, np.expand_dims(x*0.8, 2))
    # plot surface
    surface = ax.plot_surface(x, y, z, cstride=1, rstride=1, facecolors=bsl,
                           linewidth=0, antialiased=False, shade=False)
    
    
    plt.show()
    

    输出:

    【讨论】:

      猜你喜欢
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多