Removing labels, ticks, formatters in a plot with WCS projection

I want to plot a grid of images of a galaxy with matplotlib while only keeping the RA and DEC ticks/labels on the left and lower edge plots to make the figure less cluttered.

However, I have noticed that once you use the WCS projection, you cannot longer clear the axis labels, ticks or locators. This code reproduces the issue:

import matplotlib.pyplot as plt
from astropy.wcs import WCS
from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

filename = get_pkg_data_filename('galactic_center/gc_msx_e.fits')

hdu = fits.open(filename)[0]
wcs = WCS(hdu.header)

ax = plt.subplot(projection=wcs)
ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
ax.grid(color='white', ls='solid')
ax.update({'xlabel': 'Galactic Longitude', 'ylabel': ''})
ax.yaxis.set_ticklabels([], minor=True)
ax.yaxis.set_major_locator(plt.NullLocator())
plt.show()

Figure_1

I wonder if anyone has a suggestion on how to do this while keeping the projection.

Thank you

Hello @Vital-Fernandez :wave:

One of the oddities of WCSAxes is that it doesn’t use matplotlib to draw the ticks, it draw them itself. (This is primarily to rotate them with the coordinate grid and a few other nice things.) The upshot of this is that you can’t use the standard mpl API for turning them off, you need to use the WCSAxes specific API. You can find an example of that here: Ticks, tick labels, and grid lines — Astropy v5.1

hope that helps!

1 Like

Thank you very much! Sorry I didn’t see that😅

Not a problem. Very easy to miss if you don’t know what you are looking for!

1 Like