【问题标题】:Heat map on unit sphere单位球面上的热图
【发布时间】:2017-11-19 12:17:44
【问题描述】:

我想使用 python 的 matplotlib 库在单位球体上绘制热图。有几个地方讨论了这个问题。就像这样:Heat Map half-sphere plot

我可以部分做到这一点。我可以创建球体和热图。我有坐标矩阵 X、Y 和 Z,它们的大小相同。我有另一个与 X、Y 和 Z 大小相同的变量,其中包含用于创建热图的标量。但是,如果 c 在其第一行和最后一行中包含与零不同的标量,则只有一个极帽会被着色,而另一个不会被着色。生成上述结果的代码如下:

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

#Creating the theta and phi values.
theta = np.linspace(0,np.pi,100,endpoint=True)
phi   = np.linspace(0,np.pi*2,100,endpoint=True)

#Creating the coordinate grid for the unit sphere.
X = np.outer(np.sin(theta),np.cos(phi))
Y = np.outer(np.sin(theta),np.sin(phi))
Z = np.outer(np.cos(theta),np.ones(100))

#Creating a 2D matrix contains the values used to color the unit sphere.
c = np.zeros((100,100))
for i in range(100):
    c[0,i]  = 100
    c[99,i] = 100

#Creat the plot.
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.set_axis_off()
ax.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=cm.plasma(c/np.amax(c)), alpha=0.22, linewidth=1)
m = cm.ScalarMappable(cmap=cm.plasma)
m.set_array(c)
plt.colorbar(m)

#Show the plot.
plt.show()

生成的情节:

有人可以帮我看看这是怎么回事吗?

提前感谢您的帮助!

【问题讨论】:

  • 请不要循环:c[0, :] = 100 等 — 也可以尝试 c[:10, :] = 100 并看到一根杆子呈现黄色 — 这是answer below 中概述的问题,您必须了解@987654328 @2D数组位置映射到球体上,数组的哪一部分没有映射到球体上的网格
  • 啊,它是一个unit sphere,而不是一个unity球体...
  • 感谢您的评论@gboffi!我更正了统一 --> 单位。

标签: python matplotlib plot visualization heatmap


【解决方案1】:

数组中的值定义了网格的边缘。 ith 面的颜色由颜色数组中的ith 值确定。但是,对于 n 边,您只有 n-1 面,因此最后一个值将被忽略。

例如如果您有 4 个网格值和 4 种颜色,则绘图将只有网格中的前三种颜色。

因此,上述问题的解决方案是使用一个颜色数组,其中每个维度中的网格点都比网格点少一种颜色。

c = np.zeros((99,99))
c[[0,98],:] = 100

【讨论】:

    【解决方案2】:

    与您的示例有许多细微差别,但 重要的一个,即值数组c的形状。

    正如another answer 中提到的那样,网格 定义表面大于(在两个维度上都大一) 定义每个四边形补丁中的值的网格,因此由 为c 使用较小的数组可以正确选择 带颜色的不仅仅是c的开头 数组,但也关于它的目的,正如我试图证明的那样 以下。

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    from mpl_toolkits.mplot3d import Axes3D
    
    # Creating the theta and phi values.
    
    intervals = 8
    ntheta = intervals
    nphi = 2*intervals
    
    theta = np.linspace(0, np.pi*1, ntheta+1)
    phi   = np.linspace(0, np.pi*2, nphi+1)
    
    # Creating the coordinate grid for the unit sphere.
    X = np.outer(np.sin(theta), np.cos(phi))
    Y = np.outer(np.sin(theta), np.sin(phi))
    Z = np.outer(np.cos(theta), np.ones(nphi+1))
    
    # Creating a 2D array to be color-mapped on the unit sphere.
    # {X, Y, Z}.shape → (ntheta+1, nphi+1) but c.shape → (ntheta, nphi)
    c = np.zeros((ntheta, nphi)) + 0.4
    # The poles are different
    c[ :1, :] = 0.8
    c[-1:, :] = 0.8
    # as well as the zones across Greenwich
    c[:,  :1] = 0.0
    c[:, -1:] = 0.0
    
    # Creating the colormap thingies.
    cm = mpl.cm.inferno
    sm = mpl.cm.ScalarMappable(cmap=cm)
    sm.set_array([])
    
    # Creating the plot.
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=cm(c), alpha=0.3)
    plt.colorbar(m)
    
    # Showing the plot.
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2020-05-14
      • 2015-10-17
      • 2020-11-01
      • 2010-12-22
      相关资源
      最近更新 更多