4

I am trying to plot spherical circles in galactic coordinates using the SphericalCircle module in astropy. I'm having trouble wrapping galactic longitude angles between (-180, 180) degrees instead of the default (0, 360) degrees. Below is an example of what I'm trying to do.

import numpy as np
import matplotlib.pyplot as plt
from astropy import units as u
from astropy.coordinates import SkyCoord
from matplotlib.collections import PatchCollection
from astropy.visualization.wcsaxes import SphericalCircle

glat = np.random.uniform(-90, 90, 100)
glong = np.random.uniform(-180, 180, 100)
data = np.column_stack((glong, glat)) 
gal = SkyCoord(l = data[:,0] * u.deg, b = data[:,1] * u.deg, frame='galactic')
gal_long = gal.l.wrap_at(180*u.deg).deg 

fig,ax = plt.subplots(1)

all_beams = []
for i in range(len(gal)):

    beam = SphericalCircle(center=(gal_long[i] * u.deg, gal[i].b.deg * u.deg), \
                          radius = 7*u.arcmin, facecolor="None",  edgecolor="black", linewidth=2)

    all_beams.append(beam)


pc = PatchCollection(all_beams, match_original=False, lw=3,  facecolor='white', 
edgecolor='k')
ax.add_collection(pc)
ax.set_xlim(-180,360)
ax.set_ylim(-90,90)
plt.show()

Update1: Adding More Details. The line gal_long = gal.l.wrap_at(180*u.deg).deg wraps longitude between (-180, 180) but it looks like SphericalCircle by default changes it back to (0, 360). Is there a way to force SphericalCircle to return circles in the (-180, 180) convention?

Vishnu
  • 499
  • 1
  • 5
  • 23
  • 1
    Are you asking what math to use to wrap a value into the range [-180, 180]? – mkrieger1 Jul 17 '21 at 19:51
  • 1
    No, gal_long = gal.l.wrap_at(180*u.deg).deg wraps it between [-180,180] but it seems SphericalCircle takes it back to [0,360] which is why when I plot it looks different than I expect. I'll update the question with more details. – Vishnu Jul 17 '21 at 19:56
  • The problem is due to the fact that `SphericalRepresentation` does not allow negative longitudes. You should either live with it or use `plt.plot.scatter` directly. – DYZ Jul 17 '21 at 20:07
  • Using a scatter plot, is there a way to draw the marker size with radius 7 arc minutes and plot it in Galactic coordinates using matplotlib.projections? – Vishnu Jul 17 '21 at 20:45

0 Answers0