Search code examples
pythonimagematplotlibastropyfits

Plotting fits image with matplotlib.imshow with WCS as x and y axes


I was wondering if anyone knew how to plot a fits image with the python package matplotlib.imshow with the corresponding world coordinate system values or perhaps even Right Ascension or Declination as the x and y values rather than the physical pixel values, similar to the bottom plot of this page: http://astroplotlib.stsci.edu/page_images.htm

Unfortunately, the script provided is in IDL...something I am not yet proficient in...

It would probably be helpful if I outlined my gridspec layout:

fig = pyplot.figure(figsize=(11,11))

gridspec_layout = gridspec.GridSpec(3,3)
gridspec_layout.update(hspace=0.0, wspace=0.0)

hdulist_org_M33_UVM2 = fits.open('myfits.fits')
wcs = WCS(hdulist_org_M33_UVM2[0].header)

pyplot_2 = fig.add_subplot(gridspec_layout[2])

ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=wcs)

pyplot_2.add_axes(ax)

But to no luck.

Many thanks.


Solution

  • One solution might be to use the subplot parameter to FITSFigure, and obtain the bounds from your gridspec.

    Something along the following lines:

    from matplotlib import pyplot
    from matplotlib import gridspec
    import aplpy
    
    fig = pyplot.figure(figsize=(11, 11))
    gridspec_layout = gridspec.GridSpec(3, 3)
    gridspec_layout.update(hspace=0.0, wspace=0.0)
    axes = fig.add_subplot(gridspec_layout[2])
    m33 = aplpy.FITSFigure('wfpcii.fits', figure=fig,
                           subplot=list(gridspec_layout[2].get_position(fig).bounds),
                           # dimensions and slices are not normally necessary;
                           # my test-figure has 3 axes
                           dimensions=[0, 1], slices=[0])
    print(dir(gridspec_layout[2]))
    print(gridspec_layout[2].get_position(fig).bounds)
    m33.show_colorscale()
    pyplot.show()
    

    Not really pretty, but it'll work. if I come across an easier way to attach a FITSFigure directly to an axes, I'll amend this answer or put a new one.