I am creating a grid of plots with matplotlib which uses astropy World Cordinate System projection.
I would like to complete clear one of the axis (no titles, labels or ticks) but I have not been able with the traditional commands.
This script reproduces the issues:
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()
I wonder if anyone could please share any advice
Thank you.
Following @Chang Ye solution, I have adapted the code to:
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[0])
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()
However, this script produces the following issue within astropy (the plot is stil displayed but without either the desired and non-desired axes features):
Traceback (most recent call last):
File "/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_qt.py", line 455, in _draw_idle
self.draw()
File "/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 436, in draw
self.figure.draw(self.renderer)
File "/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 73, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "/anaconda3/lib/python3.8/site-packages/matplotlib/figure.py", line 2810, in draw
mimage._draw_list_compositing_images(
File "/anaconda3/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/core.py", line 464, in draw
super().draw(renderer, **kwargs)
File "/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
return draw(artist, renderer)
File "/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 3082, in draw
mimage._draw_list_compositing_images(
File "/anaconda3/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/core.py", line 45, in draw
self.axes.draw_wcsaxes(renderer)
File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/core.py", line 414, in draw_wcsaxes
coord._draw_grid(renderer)
File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/coordinate_helpers.py", line 571, in _draw_grid
self._update_grid_lines_1d()
File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/coordinate_helpers.py", line 864, in _update_grid_lines_1d
x_ticks_pos = [a[0] for a in self.ticks.pixel['b']]
KeyError: 'b'
My astropy version is 5.0.4 and the matplotlib version is 3.5.1
I found that you can do this by tricking the set_ticks method to just put ticks outside of the figure. No ticks was unfortunately not allowed (number=0)
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': ''})
lat = ax.coords[1]
lat.set_ticks([10,20]*u.deg)
plt.show()
Produces