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.
This is the 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))
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
?