绘制具有astropy wcs的RA / Dec多边形aplpy FITSFigure

时间:2017-02-24 19:11:36

标签: python matplotlib astropy aplpy spherical-coordinate

我在RA / Dec球面坐标中得到了一些多边形,我试图用aplpy和一个天文世界坐标系(WCS)对象绘制它们,但是我的脚本崩溃了。

我可以使用这个独立脚本重现问题:

#!/usr/bin/env python
import astropy
import aplpy, numpy
from numpy import array
import matplotlib.pyplot as plt
from matplotlib import cm, colors
from astropy.wcs import WCS

cent_ra  =   1 # plot center, ra,  deg
cent_dec =  88 # plot center, dec, deg
rad      =   7 # plot radius, deg
rad_pix  = 300 # plot radius, pixels

# two polygons to plot, in ra-dec space
polygons_radec = [ array([[ 1.0, 88.0],[ 2.0, 88.0],[ 2.0, 89.0],[ 1.0, 88.0]]),
                   array([[ 1.0, 88.0],[ 2.0, 88.0],[ 2.0, 87.0],[ 1.0, 88.0]]) ]
zvalues = [5,2]

# setup our radec-pixel coordinate transformation
w = WCS( naxis=2 )
w.naxis1 = 2 * rad_pix
w.naxis2 = 2 * rad_pix
w.wcs.crpix = [rad_pix, rad_pix]  # reference pixel
w.wcs.crval = [ cent_ra, cent_dec ]  # reference pixel's ra/dec coordinate?
w.wcs.cdelt = array([ rad/rad_pix, rad/rad_pix ]) # pixel delta in ra/dec space?
w.wcs.ctype = ['RA--TAN','DEC-TAN']

# convert the ra-dec polygon coordinates to pixel coordinates
polygons = []
for p in polygons_radec :
  polygon = []
  for c in p :
    polygon += [ w.wcs_world2pix( c[0], c[1], 1 ) ]
  polygons += [ array( polygon ) ]
print('polygons:')
print(polygons)
print()

# plot our polygons with colors
cmap1 = cm.YlOrBr
norm1 = colors.Normalize( numpy.min(zvalues), numpy.max(zvalues) )
fig   = aplpy.FITSFigure( w )
for p, c in zip( polygons, zvalues ) :
  fig.show_polygons( [p], facecolor=cmap1(norm1(c)), edgecolor='none', alpha=0.3 )

plt.savefig( 'plot.png' , dpi=300 )

当我尝试运行它时,我得到一个相当大的回溯/异常:

Traceback (most recent call last):
  File "./test.py", line 46, in <module>
    plt.savefig( 'plot.png' , dpi=300 )
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/pyplot.py", line 696, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/figure.py", line 1563, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py", line 203, in print_figure
    FigureCanvasAgg.print_figure(self, *args, **kwargs)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/backend_bases.py", line 2232, in print_figure
    **kwargs)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 527, in print_png
    FigureCanvasAgg.draw(self)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 62, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/.../miniconda3/lib/python3.5/site-packages/mpl_toolkits/axes_grid1/parasite_axes.py", line 295, in draw
    self._get_base_axes_attr("draw")(self, renderer)
  File "/.../miniconda3/lib/python3.5/site-packages/mpl_toolkits/axisartist/axislines.py", line 772, in draw
    super(Axes, self).draw(renderer, inframe)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 62, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 2319, in draw
    a.draw(renderer)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/artist.py", line 62, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/axis.py", line 1108, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/axis.py", line 951, in _update_ticks
    tick_tups = [t for t in self.iter_ticks()]
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/axis.py", line 951, in <listcomp>
    tick_tups = [t for t in self.iter_ticks()]
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/axis.py", line 898, in iter_ticks
    for i, val in enumerate(majorLocs)]
  File "/.../miniconda3/lib/python3.5/site-packages/matplotlib/axis.py", line 898, in <listcomp>
    for i, val in enumerate(majorLocs)]
  File "/.../miniconda3/lib/python3.5/site-packages/aplpy/labels.py", line 421, in __call__
    label = self.axis.apl_tick_spacing * self.axis.apl_tick_positions_world[ipos]
  File "/.../miniconda3/lib/python3.5/site-packages/aplpy/angle_util.py", line 242, in __mul__
    s = Angle(sexagesimal=(d, m, s), latitude=self.latitude)
  File "/.../miniconda3/lib/python3.5/site-packages/aplpy/angle_util.py", line 52, in __init__
    self._simplify()
  File "/.../miniconda3/lib/python3.5/site-packages/aplpy/angle_util.py", line 88, in _simplify
    degrees")
Exception: latitude should be between -90 and 90

该图重叠了一个球面坐标系的极点(Declination = 90度),所以我想知道是否会导致问题/显示错误,或者是否因为我和#39;我错误地设置了我的WCS。

如何让多边形出现?

0 个答案:

没有答案