我使用以下代码将 ra dec wcs 的拟合图像绘制到银河坐标上:
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
然而,图像被证明是正确转换的,但有一些原本应该没什么的额外像素。
预期结果(这使用
a.contourf
,没有问题):
a.contourf(fits.open(path_sofia)[1].data[22],transform=a.get_transform(wcs_sofia))
拟合文件链接:https://github.com/Lim1029/matplotlib_imshow_wcs_transform
所以问题是:如何获得类似第三张图像的东西,但带有
imshow
?
我找到了一个解决方法,即定义一个 matplotlib.patches.Rectangle 并对其进行变换,然后使用 set_clip_path 来裁剪不需要的区域。
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()