单位球面上的热图 [英] Heat map on unit sphere
问题描述
我想使用python的matplotlib库在单位球面上绘制热图.在几个地方讨论这个问题.就像这样:热图半球图
I would like to plot a heat map on the unit sphere using the matplotlib library of python. There are several places where this question is discussed. Just like this: Heat Map half-sphere plot
我可以部分地做到这一点.我可以创建球体和热图.我有大小相同的坐标矩阵X,Y和Z.我还有另一个与X,Y和Z大小相同的变量,其中包含用于创建热图的标量.但是,如果c的第一行和最后一行包含标量与零不同的标量,则仅一个极坐标色将被着色,而另一个则没有.代码生成上面提到的结果是下一个:
I can do this partially. I can creat the sphere and the heatplot. I have coordinate matrices X,Y and Z, which have the same size. I have another variable of the same size as X, Y and Z, which contains scalars used to creat the heat map. However in case c contains scalars differ from zero in its first and last rows, just one polar cap will be colored but not the other. The code generates the above mentioned result is the next:
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 plot which was generated:
有人可以帮我这里发生什么事吗?
Could somebody help me what's going on here?
谢谢您的帮助!
推荐答案
数组中的值定义了网格的边缘.第i
个面的颜色由颜色数组中的第i
个值确定.但是,对于n
边,您只有n-1
面,因此最后一个值将被忽略.
The values in the arrays define the edges of the grid. The color of the i
th face is determined by the i
th value in the color array. However, for n
edges you only have n-1
faces, such that the last value is ignored.
例如如果您有4个网格值和4种颜色,则绘图将仅具有网格中的前三种颜色.
E.g. if you have 4 grid values and 4 colors, the plot will have only the first three colors in the grid.
因此,上述解决方案将是使用一种颜色数组,该颜色数组在每个维度上的颜色均小于网格点的颜色.
Thus a solution for the above would be to use a color array with one color less than gridpoints in each dimension.
c = np.zeros((99,99))
c[[0,98],:] = 100
这篇关于单位球面上的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!