【问题标题】:Plotting data on unit sphere with colours用颜色在单位球体上绘制数据
【发布时间】:2018-09-19 02:46:13
【问题描述】:

您好,我有一个包含三列的数据集:theta、phi 以及 theta 和 phi 处的函数值。我想将此数据投影到一个单位球体上,以便球体上的颜色对应于函数的大小,类似于this post。我提供的样本数据的性质是球体的上半部和下半部的值是相同的,因此期望球体的上半部和下半部的颜色分布是相同的,但由于某种原因,我发现虽然形状正确,但由于某种原因,上半部分更亮(见下图)。有谁知道为什么会这样?

数据集:

Theta     Phi      Values
0.000000 -3.141592 0.500000
0.000000 -3.078126 0.500000
0.000000 -3.014659 0.500000
...    ...  ...
0.031733 -3.141592 0.499497
0.031733 -3.078126 0.499497
0.031733 -3.014659 0.499497
...    ...  ...
0.507732 -0.031733 0.388763
0.507732  0.031734 0.388763
0.507732  0.095200 0.388541
...    ...  ...
1.364530 -0.095199 0.131597
1.364530 -0.031733 0.135245
1.364530 0.031734 0.135245
1.364530 0.095200 0.131597
...    ...  ...
2.633860 -0.095199 0.388541
2.633860 -0.031733 0.388763
2.633860  0.031734 0.388763
...    ...  ...    
3.109859 3.014660 0.499497
3.109859 3.078126 0.499497
3.109859 3.141593 0.499497
...    ...  ...
3.141592 3.014660 0.500000
3.141592 3.078126 0.500000
3.141592 3.141593 0.500000

代码:

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

#theta inclination angle
#phi azimuthal angle
n_theta = 100 #number of values for theta
n_phi = 100  #number of values for phi
r = 1        #radius of sphere

theta, phi = np.mgrid[0: pi:n_theta*1j,-pi:pi:n_phi*1j ]

x = r*np.sin(theta)*np.cos(phi)
y = r*np.sin(theta)*np.sin(phi)
z = r*np.cos(theta)

print theta.shape 
print phi.shape 

inp = []
f = open("datafile.dat","r")
for line in f:
    i = float(line.split()[0])
    j = float(line.split()[1])
    val = float(line.split()[2])
    inp.append([i, j, val])

inp = np.array(inp)
#print inp
#print inp.shape

#reshape the input array to the shape of the x,y,z arrays. 
c = inp[:,2].reshape((n_phi,n_theta))

#Set colours and render
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

surf = ax.plot_surface(
    x,y,z,  rstride=1, cstride=1, facecolors=cm.hot(c), alpha=0.9, linewidth=1) 
ax.set_xlim([-2.0,2.0])
ax.set_ylim([-2.0,2.0])
ax.set_zlim([-2,2])
ax.set_aspect("equal")

plt.title('Projecting data on unit sphere')

#Label axis. 
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

#Creates array for colorbar from 0 to 1. 
a = array( [1.0, 0.5, 0.0])

#Creates colorbar
m = cm.ScalarMappable(cmap=cm.hot)
m.set_array(a)
plt.colorbar(m)
plt.savefig('Projecting data on unit sphere.png')

f.close()
plt.show()

【问题讨论】:

    标签: python-2.7 matplotlib plot heatmap


    【解决方案1】:

    您需要将plot_surfaceshade参数设置为False

    ax.plot_surface(..., shade=False)
    

    否则会在您的表面顶部模拟一些灯光效果,这取决于视角。

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多