单位范围内的热图

时间:2017-06-16 16:31:21

标签: python matplotlib plot visualization heatmap

我想使用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()

生成的情节:
The heat map on the unit sphere.

有人可以帮我解决这里发生的事情吗?

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

数组中的值定义网格的边缘。 i面的颜色由颜色数组中的i值确定。但是,对于n边,您只有n-1个面,这样就会忽略最后一个值。

E.g。如果您有4个网格值和4种颜色,则绘图将只有网格中的前三种颜色。 enter image description here

因此,上述方法的解决方案是使用每个维度中一种颜色小于网格点的颜色数组。

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

答案 1 :(得分:0)

你的例子有一些小的差别,但是 重要的一个,即数组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()

enter image description here

相关问题