将matplotlib的hexbin函数应用于Aitoff投影

5

我有一个漂亮的六边形图,但我想知道是否有任何方法将六边形图投影到Aitoff上?关键代码如下:

import numpy as np
import math
import matplotlib.pyplot as plt
from astropy.io import ascii

filename = 'WISE_W4SNRge3_and_W4MPRO_lt_6.0_RADecl_nohdr.dat'
datafile= path+filename
data = ascii.read(datafile)  
points = np.array([data['ra'], data['dec']])

color_map = plt.cm.Spectral_r 
points = np.array([data['ra'], data['dec']]) 
xbnds = np.array([ 0.0,360.0]) 
ybnds = np.array([-90.0,90.0]) 
extent = [xbnds[0],xbnds[1],ybnds[0],ybnds[1]] 

fig = plt.figure(figsize=(6, 4)) 
ax = fig.add_subplot(111) 
x, y = points 
gsize = 45 
image = plt.hexbin(x,y,cmap=color_map, 
    gridsize=gsize,extent=extent,mincnt=1,bins='log') 

counts = image.get_array() 
ncnts = np.count_nonzero(np.power(10,counts)) 
verts = image.get_offsets() 

ax.set_xlim(xbnds) 
ax.set_ylim(ybnds) 
plt.xlabel('R.A.')  
plt.ylabel(r'Decl.') 
plt.grid(True) 
cb = plt.colorbar(image, spacing='uniform', extend='max') 
plt.show()

我已经尝试过:

plt.subplot(111, projection="aitoff")

在执行plt.hexbin命令之前,需要进行一些操作,但这只会生成一个漂亮但空白的Aitoff网格。

enter image description here


原则上,在任何投影中绘制hexbin图形没有问题。如果没有提供最小完整可再现示例(mcve),没有人可以知道你的问题出在哪里。 - ImportanceOfBeingErnest
这是(希望)最小、完整的代码。RA/Decl或x,y数据点可以使用上面给出的numpy数组。 - npross
基本上我找不到使用hexbin进行Aitoff投影的例子,所以我认为这并不像“在任何投影中绘制hexbin图”那样容易。 - npross
2个回答

5
问题在于Aitoff投影使用弧度,从-π到+π。而不是从0到360度。我使用了Angle.wrap_at函数来实现这一点,具体可以参考Astropy的这个示例(该示例基本上告诉您如何创建一个正确的Aitoff投影图)。
此外,你不能更改轴限制(否则会导致错误),也不应该使用extent(就像ImportanceOfBeingErnest的答案中指出的那样)。
您可以按照以下方式更改代码以获得所需结果:
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import ascii
from astropy.coordinates import SkyCoord
from astropy import units

filename = 'WISE_W4SNRge3_and_W4MPRO_lt_6.0_RADecl_nohdr.dat'
data = ascii.read(filename)
coords = SkyCoord(ra=data['ra'], dec=data['dec'], unit='degree')
ra = coords.ra.wrap_at(180 * units.deg).radian
dec = coords.dec.radian

color_map = plt.cm.Spectral_r
fig = plt.figure(figsize=(6, 4))
fig.add_subplot(111, projection='aitoff')
image = plt.hexbin(ra, dec, cmap=color_map,
                   gridsize=45, mincnt=1, bins='log')

plt.xlabel('R.A.')
plt.ylabel('Decl.')
plt.grid(True)
plt.colorbar(image, spacing='uniform', extend='max')
plt.show()

这提供了一个enter image description here


1
我猜你的问题在于使用了extent,它被设置为球坐标系范围之外的值。
下面的代码可以正常工作:
import matplotlib.pyplot as plt
import numpy as np

ra = np.linspace(-np.pi/2.,np.pi/2.,1000)
dec = np.sin(ra)*np.pi/2./2.
points = np.array([ra, dec]) 

plt.subplot(111, projection="aitoff")

color_map = plt.cm.Spectral_r 
x, y = points 
gsize = 45 
image = plt.hexbin(x,y,cmap=color_map, 
                   gridsize=45,mincnt=1,bins='log') 

plt.xlabel('R.A.')  
plt.ylabel(r'Decl.') 
plt.grid(True) 
cb = plt.colorbar(image, spacing='uniform', extend='max') 
plt.show()

enter image description here


嗯...所以我可以让你的代码工作并重现上面的图形。但我做不到的是,拿我的astropy.table却得不到任何有意义的东西。 - npross
我已经修改了上面的代码。数据在这里: https://www.dropbox.com/sh/4qsv753bhksn6ur/AAB4kthAcvmx_kQRa0zhkhQ0a?dl=0 - npross
我没有安装astropy。 - ImportanceOfBeingErnest
pip3安装astropy - npross

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接