How to plot a FITS image with world coordinates of RA DEC on galactic coordinates

368 Views Asked by At

I use the following code to plot a fits image with ra dec wcs onto a galactic coordinate:

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

fig = plt.figure()
path_galactic = "w40_c18o.fits"
wcs_galactic = WCS(fits.open(path_galactic)[0].header).celestial[0:2] #my reference coordinate

path_sofia = 'F0248_FI_IFS_8700081_RED_WXY_100061-100115.fits'
wcs_sofia = WCS(fits.open(path_sofia)[1].header).celestial[0:2] #this is in ra dec

a = fig.add_axes([0, 0, 1, 1], projection=wcs_galactic) # set the projection of the plot galactic
a.set_aspect("equal")
a.imshow(fits.open(path_sofia)[1].data[22],transform=a.get_transform(wcs_sofia)) #plot the sofia image with transform to galactic

However, the image turned out to be transformed correctly, but with additional pixels that supposed to be nothing. imshow showing error

This is the original image in its own WCS original image in its own WCS

Expected outcome (this uses a.contourf, which doesn't have the issue):

a.contourf(fits.open(path_sofia)[1].data[22],transform=a.get_transform(wcs_sofia))

Using contourf doesn't seem to have the same issue

Link to the fits files: https://github.com/Lim1029/matplotlib_imshow_wcs_transform

So the question is: How to have something like third image, but with imshow?

1

There are 1 best solutions below

0
ming On

I have found a workaround, that is, to define a matplotlib.patches.Rectangle and transform it, then use set_clip_path to clip the unwanted area.

plot1 = ax.imshow(fits_image[0].data, ..., transform=ax.get_transform(astropy.wcs.WCS(fits_image[0].header)))    
boxframe = matplotlib.patches.Rectangle(...<You have to find the coordinates of the corners of the image first, in pixel unit>..., transform=ax.get_transform(astropy.wcs.WCS(fits_image[0].header)))
plot1.set_clip_path(boxframe)

...
plt.show()