制作天空坐标的二维直方图

时间:2015-04-13 05:03:46

标签: python numpy matplotlib

我试图使用直方图检查样本的角度分布。 我所做的是将数据文件作为ndarray读取,其中包含具有天空坐标的字段(' alpha''' delta)然后我使用numpy.histogram2d和imshow来制作剧情。 当我将最终的情节与数据范围进行比较时,我发现它们的不同。 这是一个在整个范围内具有分箱的示例代码:

import numpy as np
import matplotlib.pyplot as pl
#Generate random catalogue
alpha=100*np.random.random(100)
delta=-30+60*np.random.random(100)
CATALOGUE=np.ndarray(shape=(100),dtype={'names':('alpha','delta'),'formats':('f8','f8')})
for i in range(100):
    CATALOGUE[i]=(alpha[i],delta[i])

print "Right Ascension interval= [%s,%s]"%(np.min(CATALOGUE['alpha']),np.max(CATALOGUE['alpha']))
print "Declination interval= [%s,%s]"%(np.min(CATALOGUE['delta']),np.max(CATALOGUE['delta']))
H, xedges, yedges = np.histogram2d(CATALOGUE['alpha'],CATALOGUE['delta'], bins=(np.arange(0,360+0.1,3.6),np.arange(-90,90+0.1,1.8)))
fig = pl.figure(figsize=(9,5))
ax = fig.add_subplot(111)
im = pl.imshow(H, interpolation='none',cmap=pl.cm.gray_r,extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])
fig.colorbar(im,orientation='Horizontal')
ax.set_aspect('equal')
pl.savefig('hist_test.png')

当我运行时,直方图与初始数据给出的间隔预期不同。我试图改变分档但我没有取得更好的结果。

我可能做错了什么?

有没有办法在像表面投射的球体上完成此操作?

感谢您的帮助

编辑:这是一个非常小的数据样本(删除了其他列)

321.35969128 9.93335781 322.61418111 10.14449632 325.01105374 10.53199921 325.20021403 10.49549834 325.21419238 10.41766926 325.30530102 10.56187662 325.42751521 10.44816114 325.44439994 10.47945994 325.46404052 10.56886508 325.47326548 10.53406801 325.48491199 10.54833508 325.51330280 10.52523922 325.53097826 10.66066162 325.57284098 10.59885846 325.63550236 10.60901457 325.84671192 10.68180839 325.78860114 10.65201938 325.85162465 10.54814596 325.79754828 10.54660071 325.93804346 10.62466427

1 个答案:

答案 0 :(得分:2)

matplotlib的hist2d会为你工作吗?:

import numpy as np
import matplotlib.pyplot as pl

#Generate random catalogue
# Range for unif(a,b) is (b-a)*unif() + a
alpha=360*np.random.random(100) # alpha is (0,360)
delta=(90+90)*np.random.random(100) - 90 #delta is (-90,0)
CATALOGUE=np.ndarray(shape=(100),dtype={'names':('alpha','delta'),'formats':('f8','f8')})
for i in range(100):
    CATALOGUE[i]=(alpha[i],delta[i])

print "alpha interval= [%s,%s]"%(np.min(CATALOGUE['alpha']),np.max(CATALOGUE['alpha']))
print "delta interval= [%s,%s]"%(np.min(CATALOGUE['delta']),np.max(CATALOGUE['delta']))
fig = pl.figure(figsize=(9,5))
ax = fig.add_subplot(111, aspect='equal')

#Non-uniform bins:
pl.hist2d(alpha, delta, bins=[[0,20,40,45,48,52,60,80,100,200,225,250,360],[-90,-30, -12,-4,2,18,30,90]])
#Uniform bins:
#pl.hist2d(alpha, delta, bins=[np.arange(0,360,3.6),np.arange(-90,90,1.8)])
pl.colorbar()
pl.show()

样本结果:

alpha interval= [4.62764250657,359.013680439]
delta interval= [-87.9255707577,88.9034353411]

enter image description here