Vizualize areas in the sky
See original GitHub issueDescription
Not sure if this is already possible, but I tried looking around and it doesn’t seem to me a clear feature in (pure) astropy. I have only found code that uses WCS coordinates from existing data files or relatively complex code like the Fermi GBM tools. Also the affiliated package “astropy regions” seems to be more complicated than what I would expect.
Is it possible to plot an area in the sky (like a rectangle or a circle or any area defined by angle ranges) using pure astropy without existing FITS file headers?
Suppose I work from an “aitoff” projection in equatorial coordinates.
I can plot single SkyCoord objects like the Crab Nebula or multiple ones based on some condition like the Galactic Plane. What if I want to show, e.g.:
- a stripe of few degrees along the galactic plane
- a circle of fiew degrees around the Crab position
- the accessible FoV from an EarthLocation
Of course, I would expect this to work even if I transform from icrs to galactic and viceversa…
Additional context
I tried to do naïvely this
(Of course if there is nicer way to write this code I am all ears.
Thank you in advance)
from astropy import units as u
from astropy.coordinates import SkyCoord
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
# Define sources
vernal_point = SkyCoord(ra=0*u.degree, dec=0*u.degree, frame='icrs')
ecliptic = SkyCoord(lon=np.arange(-180,180), lat=0*u.deg, unit=u.deg, frame='barycentricmeanecliptic')
galactic_plane = SkyCoord(l=np.arange(-180,180), b=0, unit=u.deg, frame="galactic")
celestial_equator = SkyCoord(ra=np.arange(-180,180)*u.deg, dec=0*u.deg, frame='icrs')
crab = SkyCoord.from_name("Crab Nebula")
plt.figure(figsize=(8,6))
plt.subplot(111, projection="aitoff")
ax=plt.gca()
plt.grid(True)
plot_options = {"marker":"o"}
def plot_sky_coordinates(skycoord,frame="icrs",**kwargs):
c = skycoord.transform_to(frame)
if frame=="galactic":
x=c.l
y=c.b
elif frame=="icrs":
x=c.ra
y=c.dec
else:
raise ValueError("Only supported coordinate frames are 'icrs' and 'galactic'")
plt.plot(-x.wrap_at(180 * u.deg).radian, y.radian, **kwargs)
def fill_patch(origin, frame="icrs", ax=None, height=None, width=None, shape="rectangle", **kwargs):
c = SkyCoord(origin[0], origin[1], unit=u.deg).transform_to(frame)
if frame=="galactic":
x=c.l
y=c.b
elif frame=="icrs":
x=c.ra
y=c.dec
else:
raise ValueError("Only supported coordinate frames are 'icrs' and 'galactic'")
o = (x.wrap_at(180 * u.deg).radian,y.radian)
if shape=="rectangle":
p = Rectangle(o,
(width*u.deg).to_value("rad"),
(height*u.deg).to_value("rad"), angle=0.0, alpha=0.5)
ax.add_patch(p)
frame = "icrs"
plot_sky_coordinates(vernal_point,**plot_options, label="Vernal point", ls="", frame=frame)
plot_sky_coordinates(crab,label="Crab Nebula", ls="",marker="*", frame=frame)
plot_sky_coordinates(celestial_equator,label="Celestial equator", ls="dashed", frame=frame)
plot_sky_coordinates(ecliptic,label="Ecliptic", ls="dashed", frame=frame)
plot_sky_coordinates(galactic_plane,label="Galactic plane", ls="dashed", frame=frame)
fill_patch((-180,-60), frame=frame, ax=ax, height=120, width=360, shape="rectangle", alpha=0.2, label="patch")
if frame=="galactic":
plt.suptitle("Galactic coordinates")
plt.xlabel("Galactic longitude (deg)")
plt.ylabel("Galactic latitude (deg)")
elif frame in ["icrs", "fk5"]:
plt.suptitle("Equatorial coordinates")
plt.xlabel("Right Ascension (deg)")
plt.ylabel("Declination (deg)")
plt.subplots_adjust(top=1.2,bottom=0.0)
plt.legend(loc='best', bbox_to_anchor=(0.75, 0.5, 0.5, 0.5))
plt.show()
and I obtain the following (but I have likely messed up something along the way)
Issue Analytics
- State:
- Created a year ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
Not really…that tutorial shows what I am already able to do: plotting and transforming single coordinates on the sky - what I’d like to do is similar to this
or this
or this
but without the need of real data.
WCSAxes
adds tick labels when grid lines intersect the frame of the plot, but that never happens with a full-sky Aitoff projection and the defaultRectangularFrame
. I have revised my example above to use the alternativeEllipticalFrame
(see also https://docs.astropy.org/en/stable/visualization/wcsaxes/custom_frames.html).