I have the following code to plot several pairs of galactic coordinates, but would like for the x-axis to show the full degree range instead of negative numbers for angles greater than 180. Suggestions? Am I just overlooking a SkyCoord parameter?
Working example:
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
from astropy import units as u
from astropy.coordinates import SkyCoord
plt.rcParams['figure.figsize'] = [8,8] # [width, height]
df = pd.read_csv('sight_lines_coord.txt', delimiter='\s+', usecols=['l', 'b'])
stars = SkyCoord(df['l'], df['b'], frame='galactic', unit=u.deg)
plt.scatter(stars.l.wrap_at('180d'), stars.b)
plt.grid(True)
The data frame consists of two columns: longitude, latitude (ex. 325.0, -5.2)
I should have added this earlier, my apologies. If I use wrap_at('360d') I get this plot, which is not what I want.
Wrapping at 180 gives me the picture I want, I just need my xtick labels to be non-negative.
pyplot.xticks
is the function for this. Called with no arguments, it returns a tuple,(ticks, labels)
.ticks
is the x coordinate of all current x-axis ticks, andlabels
is what they are labeled with. If called with two arguments, it sets the tick values.There's one caveat, which is that sometimes the
labels
you get from pyplot.xticks() are empty until after you call plt.show() (I'm not exactly sure why). That means you should only useticks
to set the labels.Inverting the X-axis would look something like:
Again, called with no arguments, it returnsthe left boundary, then the right boundary. Called with two arguments, it sets the left and right boundaries. By flipping them, we can invert the axes. Incorporating that into the code above gives me:
OLD ANSWER:
You use
Angle.wrap_at
(docs here) which I think is the root of your problem.stars.l.wrap_at('180d')
will coerce all angles in your array to be within -180 <= angle < 180. For your case, you want the array to be wrapped such that 0 <= angle < 360, which would be accomplished bystars.l.wrap_at('360d')
.