question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vizualize areas in the sky

See original GitHub issue

Description

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)

image image

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
HealthyPearcommented, Oct 10, 2022

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.

0reactions
ayshihcommented, Oct 11, 2022

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 default RectangularFrame. I have revised my example above to use the alternative EllipticalFrame (see also https://docs.astropy.org/en/stable/visualization/wcsaxes/custom_frames.html).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interactive Sky Chart | Map Your Night Sky - Sky & Telescope
The interactive sky chart offers a lot in the way of customizing what you want to see. Chart the stars and planets visible...
Read more >
Online Planetarium - Interactive Sky Chart | TheSkyLive.com
Interactive sky chart showing planets, asteroids, comets and interplanetary probes and how to find them from your location.
Read more >
Sky Map in Real Time - Stelvision
An online sky map to identify constellations and major stars and planets easy to see with the naked eye.
Read more >
Use Google Sky Maps - Google Maps Help
Google Sky Maps is a celestial map that shows you objects like stars, constellations, galaxies, planets, or the Earth's moon. To use Google...
Read more >
Your Sky - Fourmilab
Your Sky provides three ways to view the sky with links, where appropriate ... observing locations, display options, and orbital elements of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found