<p>Looking at the plot you have, I notice that there is a concentration of dots at the "poles".  The concentration should be along the equator, and particularly towards the centre of the galaxy.</p>

<p>I think you have your axes switched, so points along the galactic equator are being drawn along the vertical "prime meridian".  </p>

CC BY-SA 4.0
2019-07-17T07:44:54.650Z
2019-07-17T07:44:54.650Z
James K
<pre><code>from astropy.io import fits    
from astropy import wcs    
import matplotlib.pyplot as plt

x = fits.open('f160w_noopt_drz.fits')['SCI']
y = wcs.WCS(x.header)

plt.subplots(figsize=(8, 8), subplot_kw={'projection':y})
plt.imshow(x.data, cmap='viridis', origin='lower', vmin=0, vmax=10)
plt.show()
</code></pre>

CC BY-SA 4.0
2019-07-12T00:03:34.050Z
Can you explain what the OP has done wrong, and what your code does to fix it please.
James K
CC BY-SA 4.0
2019-07-16T16:37:39.260Z
I'm a bit confused by this code--my data is not in a .fits file. Also, I am able to make my plot colored and whatnot, it is just the positioning of the stars in my plot that I'm not sure how to fix. Is there a way to convert from csv to fits?
Maria
CC BY-SA 4.0
2019-07-16T17:26:44.780Z
Yes, but it might not be straightforward.
Kornpob Bhirombhakdi
CC BY-SA 4.0
2019-07-11T16:01:02.827Z
2019-07-11T17:15:10.823Z
2019-07-11T17:15:10.823Z
Kornpob Bhirombhakdi
<p>This code reads coordinates as equatorial (ra, dec) and transforms them to galactic (l, b):</p>

<pre><code>eq = SkyCoord(xarr[:], yarr[:], unit=u.deg)
gal = eq.galactic
</code></pre>

<p>The contents of 'galacticwperiod.csv' are already in galactic coordinates and should not be transformed.
Something like this may give better results:</p>

<pre><code>gal = SkyCoord(xarr[:], yarr[:], frame='galactic', unit=u.deg)
</code></pre>

<p>The other issue is that pyplot's geographic projections seem to expect angles in radians.
This code:</p>

<pre><code>plt.subplot(111, projection='aitoff')
plt.grid(True)
plt.scatter(gal.l.wrap_at('180d').radian, gal.b.radian)
</code></pre>

<p>produces this plot:</p>

<p><img src="https://i.sstatic.net/2qQiC.png" alt="Successful plot in Aitoff projection"></p>

CC BY-SA 4.0
2019-07-12T01:11:48.583Z
2019-07-17T07:39:57.533Z
2019-07-17T07:39:57.533Z
Mike G
<p>I want to make a projection of millisecond pulsars on the galactic plane, much like this one from <a href="https://www.esa.int/gsp/ACT/projects/pulsar-navigation.html" rel="nofollow noreferrer">Sala et al. 2004</a>: 
<a href="https://i.sstatic.net/8sUOm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8sUOm.png" alt="From Josep Sala, &quot;Feasibility Study for a Spacecraft Navigation System...&quot;"></a></p>

<p>I have tried several methods, and have not gotten anywhere. This is my current code, along with what it is producing:</p>

<pre><code>import math as m
import numpy as np
import csv
import matplotlib.pyplot as plt
import pandas as pd
import astropy.coordinates as coord
import astropy.units as u
from astropy.io import ascii
from astropy.coordinates import SkyCoord

data = pd.read_csv('galacticwperiod.csv')

xarr = np.array(data.iloc[:,0])
yarr = np.array(data.iloc[:,1])
eq = SkyCoord(xarr[:], yarr[:], unit=u.deg)
gal = eq.galactic

#print(xarr)
#xarr = np.array(df.iloc[:,0])
#yarr = np.array(df.iloc[:,1])
#zarr = np.array(df.iloc[:,2])

#ra = coord.Angle(xarr[:], unit=u.hour)
#ra.degree
#ra = ra.wrap_at(180*u.degree)
#dec = coord.Angle(yarr[:], unit=u.deg)
#print(ra)

plt.figure(figsize=(6,5))
fig = plt.figure()
ax = fig.add_subplot(111, projection="aitoff")
plt.plot(gal.l.wrap_at(180*u.deg), gal.b.wrap_at(180*u.deg), linestyle='None')
ax.scatter(gal.l, gal.b, linestyle='None')


#ax.set_facecolor('xkcd:battleship grey')
#fig.patch.set_facecolor('white')
#ax.tick_params(axis='both', which='major', labelsize=10)
#ax.grid(color='b', linestyle='solid')
fig.show()
#plt.savefig('millisecondcoloraitoff.png', dpi=600)

</code></pre>

<p>Here are a few lines of the input file 'galacticwperiod.csv':</p>

<pre><code>Gl,Gb
111.383,-54.849
305.898,-44.888
305.913,-44.877
</code></pre>

<p>This is the image it produces:
<a href="https://i.sstatic.net/FowhM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FowhM.png" alt="My plot"></a></p>

<p>I am almost certain this is wrong because they are not distributed along the galactic plane, which they should be. The data I am using is from the <a href="http://www.atnf.csiro.au/research/pulsar/psrcat/" rel="nofollow noreferrer">ATNF Pulsar Catalog</a>. </p>

<p>These are the sites I have already looked at, for reference:</p>

<p><a href="https://stackoverflow.com/questions/33105898/astropy-matplotlib-and-plot-galactic-coordinates">https://stackoverflow.com/questions/33105898/astropy-matplotlib-and-plot-galactic-coordinates</a>
<a href="https://astropy4scipy2014.readthedocs.io/_static/notebooks/06_Celestial_Coordinates_solutions.html" rel="nofollow noreferrer">https://astropy4scipy2014.readthedocs.io/_static/notebooks/06_Celestial_Coordinates_solutions.html</a></p>

<p>Any help with this would be greatly appreciated. </p>

<coordinate><python><astropy>
How do I plot galactic coordinates using matplotlib and astropy in python?
Maria
# How do I plot galactic coordinates using matplotlib and astropy in python?

(Asked by: username_0 on 2019-07-10)

I want to make a projection of millisecond pulsars on the galactic plane, much like this one from Sala et al. 2004: 

I have tried several methods, and have not gotten anywhere. This is my current code, along with what it is producing:
import math as m
import numpy as np
import csv
import matplotlib.pyplot as plt
import pandas as pd
import astropy.coordinates as coord
import astropy.units as u
from astropy.io import ascii
from astropy.coordinates import SkyCoord

data = pd.read_csv('galacticwperiod.csv')

xarr = np.array(data.iloc[:,0])
yarr = np.array(data.iloc[:,1])
eq = SkyCoord(xarr[:], yarr[:], unit=u.deg)
gal = eq.galactic

#print(xarr)
#xarr = np.array(df.iloc[:,0])
#yarr = np.array(df.iloc[:,1])
#zarr = np.array(df.iloc[:,2])

#ra = coord.Angle(xarr[:], unit=u.hour)
#ra.degree
#ra = ra.wrap_at(180*u.degree)
#dec = coord.Angle(yarr[:], unit=u.deg)
#print(ra)

plt.figure(figsize=(6,5))
fig = plt.figure()
ax = fig.add_subplot(111, projection="aitoff")
plt.plot(gal.l.wrap_at(180*u.deg), gal.b.wrap_at(180*u.deg), linestyle='None')
ax.scatter(gal.l, gal.b, linestyle='None')


#ax.set_facecolor('xkcd:battleship grey')
#fig.patch.set_facecolor('white')
#ax.tick_params(axis='both', which='major', labelsize=10)
#ax.grid(color='b', linestyle='solid')
fig.show()
#plt.savefig('millisecondcoloraitoff.png', dpi=600)


Here are a few lines of the input file 'galacticwperiod.csv':
Gl,Gb
111.383,-54.849
305.898,-44.888
305.913,-44.877

This is the image it produces:

I am almost certain this is wrong because they are not distributed along the galactic plane, which they should be. The data I am using is from the ATNF Pulsar Catalog. 
These are the sites I have already looked at, for reference:
https://stackoverflow.com/questions/33105898/astropy-matplotlib-and-plot-galactic-coordinates
https://astropy4scipy2014.readthedocs.io/_static/notebooks/06_Celestial_Coordinates_solutions.html
Any help with this would be greatly appreciated.

**Tags:** coordinate, python, astropy

---

## 3 answers

### Answer by username_3 on 2019-07-12. Score: 7 | Accepted answer

This code reads coordinates as equatorial (ra, dec) and transforms them to galactic (l, b):
eq = SkyCoord(xarr[:], yarr[:], unit=u.deg)
gal = eq.galactic

The contents of 'galacticwperiod.csv' are already in galactic coordinates and should not be transformed.
Something like this may give better results:
gal = SkyCoord(xarr[:], yarr[:], frame='galactic', unit=u.deg)

The other issue is that pyplot's geographic projections seem to expect angles in radians.
This code:
plt.subplot(111, projection='aitoff')
plt.grid(True)
plt.scatter(gal.l.wrap_at('180d').radian, gal.b.radian)

produces this plot:


---

### Answer by username_1 on 2019-07-17. Score: 0

Looking at the plot you have, I notice that there is a concentration of dots at the "poles". The concentration should be along the equator, and particularly towards the centre of the galaxy.

I think you have your axes switched, so points along the galactic equator are being drawn along the vertical "prime meridian".


---

### Answer by username_2 on 2019-07-11. Score: -1

from astropy.io import fits    
from astropy import wcs    
import matplotlib.pyplot as plt

x = fits.open('f160w_noopt_drz.fits')['SCI']
y = wcs.WCS(x.header)

plt.subplots(figsize=(8, 8), subplot_kw={'projection':y})
plt.imshow(x.data, cmap='viridis', origin='lower', vmin=0, vmax=10)
plt.show()

> **Comments:**
> **username_1**: Can you explain what the OP has done wrong, and what your code does to fix it please.  
> **username_0**: I'm a bit confused by this code--my data is not in a .fits file. Also, I am able to make my plot colored and whatnot, it is just the positioning of the stars in my plot that I'm not sure how to fix. Is there a way to convert from csv to fits?  
> **username_2**: Yes, but it might not be straightforward.  

---

