【问题标题】:Putting matplotlib hexbin into an Aitoff projection将 matplotlib hexbin 放入 Aitoff 投影
【发布时间】:2017-09-20 11:22:16
【问题描述】:

我在下面有漂亮的 hexbin 图,但我想知道是否有任何方法可以将 hexbin 放入 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 网格。

【问题讨论】:

  • 原则上在任何投影中绘制hexbin 绘图都没有问题。没有minimal reproducible example,没人知道你的问题出在哪里。
  • 是(希望)最小的完整代码。如上所示,RA/Decl 或 x,y 数据点可以只是一个 numpy 数组。
  • 基本上没有我能找到将 hexbin 用于 Aitoff 投影的示例,所以我认为这并不像使用“在任何投影中绘制 hexbin 图...”那么简单。

标签: python matplotlib map-projections astropy hexagonal-tiles


【解决方案1】:

问题在于 Aitoff 投影使用弧度,从 -π 到 +π。不是从 0 到 360 的度数。我使用 Angle.wrap_at 函数来实现这一点,就像 this Astropy example 一样(它基本上告诉你如何创建适当的 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()

这给了

【讨论】:

    【解决方案2】:

    我猜你的问题在于使用了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()
    

    【讨论】:

    • 嗯......所以我可以得到你的代码,例如工作和重现上面的情节。我不能做的是拿走我的 astropy.table 并得到任何明智的东西。
    • 我已经修改了上面的代码。数据在这里::dropbox.com/sh/4qsv753bhksn6ur/AAB4kthAcvmx_kQRa0zhkhQ0a?dl=0
    • 我没有 astropy 可用。
    • > pip3 install astropy
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多