Initializing axes

As in Matplotlib, there are several ways you can initialize the WCSAxes.

As shown in the rest of the documentation, the simplest way is to make use of the wcsaxes.WCS class (instead of astropy.wcs.WCS) and pass this to the add_subplot() method:

from astropy.wcs import WCS
import matplotlib.pyplot as plt

wcs = WCS(...)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=wcs)


If you normally make plots directly with pyplot directly instead of creating axes and figure instances, you can do:

plt.subplot(1, 1, 1, projection=wcs)

Note that this also works with add_axes() and axes(), e.g.:

ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], projection=wcs)


plt.axes([0.1, 0.1, 0.8, 0.8], projection=wcs)

Note that in the above example, there is no explicit call to the wcsaxes package. This is because the astropy.wcs.WCS class knows about wcsaxes, and is able to automatically instantiate a wcsaxes.WCSAxes object.

Note that any additional arguments passed to add_subplot(), add_axes(), subplot(), or axes(), such as slices or frame_class, will be passed on to the WCSAxes class.


As an alternative to the above methods of initializing WCSAxes, you can also instantiate WCSAxes directly and add it to the figure:

from astropy.wcs import WCS
from wcsaxes import WCSAxes
import matplotlib.pyplot as plt

wcs = WCS(...)

fig = plt.figure()

ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=wcs)
fig.add_axes(ax)  # note that the axes have to be explicitly added to the figure

Note that in this example, we can use astropy.wcs.WCS (but wcsaxes.WCS will also work).