Astropy/SkyCoord: How do I get the x-axis to show full degree range instead of negative numbers for angles > 180?

385 views Asked by At

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)

This is how my plot looks. enter image description here

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.

enter image description here

Wrapping at 180 gives me the picture I want, I just need my xtick labels to be non-negative.

1

There are 1 answers

6
Kaia On BEST ANSWER

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, and labels 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 use ticks to set the labels.

plt.figure()
x = np.arange(-30, 30, 1)
plt.plot(x, np.sin(x/10))
ticks, labels = plt.xticks()

for tick, label in zip(ticks, labels):
    tick_value = tick if (tick >= 0) else tick + 360
    label.set_text(str(tick_value))
    
plt.xticks(ticks, labels)

Plot generated by the above code, ticks ranging from 320 to 350, then jumping to 0 up to 40


Inverting the X-axis would look something like:

lower, upper = plt.xlim()
plt.xlim(upper, lower)

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:

The same plot as the above, but the x axis has been inverted


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 by stars.l.wrap_at('360d').