1 year ago

#333840

test-img

Delosari

Matplotlib axes labels are lost if clearing axes while using astropy WCS

I wonder if anyone could please share some insight on this issue of mine:

I am making some interactive plots with matplotlib which I need to clear/redraw according to the user events.

However, if the projection WCS (world coordinate system) is not the default, I lose the axis labels and I cannot find the way to restore them.

The code above reproduces the expected behaviour (the code redraws the image several times and then closes it):

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)
print(type(wcs))
fig = plt.figure()
ax = fig.add_subplot(projection=None)

for i in range(10):
    ax.clear()
    ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
    ax.update({'xlabel': 'Galactic Longitude', 'ylabel': 'Galactic Latitude'})
    fig.canvas.draw()
    plt.pause(0.1)
plt.close(fig)

The displayed image keeps its axes labels:

Non WCS image keeps its labels

However, if we use the astropy world coordinate system we have:

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)
fig = plt.figure()
ax = fig.add_subplot(projection=wcs)

for i in range(10):
    ax.clear()
    ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
    ax.update({'xlabel': 'Galactic Longitude', 'ylabel': 'Galactic Latitude'})
    fig.canvas.draw()
    plt.pause(0.1)
plt.close(fig)

enter image description here

How could I avoid this issue or restore the labels?

Thank you

python

matplotlib

plot

projection

astropy

0 Answers

Your Answer

Accepted video resources