使用相同的投影在图像上绘制直线

2024-04-26 09:15:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用.fits文件(天文图像)绘制图,我遇到了两个问题,我认为它们是相关的:

使用astropy的这个例子:

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

fits_file = 'http://data.astropy.org/tutorials/FITS-images/HorseHead.fits'
image_file = download_file(fits_file, cache=True)
hdu = fits.open(image_file)[0]
wcs = WCS(hdu.header)

fig = plt.figure()
fig.add_subplot(111, projection=wcs)
plt.imshow(hdu.data, origin='lower', cmap='cubehelix')
plt.xlabel('RA')
plt.ylabel('Dec')
plt.show()

我可以生成这个图像:

enter image description here

现在我想用与图像相同的坐标绘制一些点:

^{pr2}$

但是,当我这样做时:

enter image description here

我正在绘制像素坐标。此外,图像不再与帧大小匹配(尽管坐标看起来很好)

关于如何处理这些问题有什么建议吗?在


Tags: from图像imageimportdatadownloadfig绘制
1条回答
网友
1楼 · 发布于 2024-04-26 09:15:37

绘制给定的坐标很容易。你所要做的就是应用一个^{}。在

我复制了您的示例,并添加了注释,其中我更改了一些内容,以及更改的原因。在

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

fits_file = 'http://data.astropy.org/tutorials/FITS-images/HorseHead.fits'
image_file = download_file(fits_file, cache=True)

# Note that it's better to open the file with a context manager so no
# file handle is accidentally left open.
with fits.open(image_file) as hdus:
    img = hdus[0].data
    wcs = WCS(hdus[0].header)

fig = plt.figure()

# You need to "catch" the axes here so you have access to the transform-function.
ax = fig.add_subplot(111, projection=wcs)
plt.imshow(img, origin='lower', cmap='cubehelix')
plt.xlabel('RA')
plt.ylabel('Dec')

# Apply a transform-function:
plt.scatter(85, -2, color='red', transform=ax.get_transform('world'))

结果是:

enter image description here

请注意,如果希望画布仅显示图像的区域,请稍后再次应用限制:

^{pr2}$

它给出了:

enter image description here

这里还有一个旁注。AstroPy有一个很好的单位支持,所以不用把arcmins和arcsecs转换成度数,你只需定义“单位”。不过,您仍然需要进行转换:

from astropy import units as u
x0 = 85 * u.degree + 20 * u.arcmin
y0 = -(2 * u.degree + 25 * u.arcmin)
plt.scatter(x0, y0, color='red', transform=ax.get_transform('world'))

enter image description here

相关问题 更多 >