Python 使用相同投影在图像上绘制线
我想使用.fits文件(天文图像)绘制一幅图,我遇到了两个问题,我认为它们是相关的: 使用astropy中的此示例:Python 使用相同投影在图像上绘制线,python,image,matplotlib,astronomy,astropy,Python,Image,Matplotlib,Astronomy,Astropy,我想使用.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-
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()
我可以生成此图像:
现在,我想使用与图像相同的坐标绘制一些点:
plt.scatter(85, -2, color='red')
但是,当我这样做时:
我在像素坐标上绘图。此外,图像不再匹配帧大小(尽管坐标看起来很好)
有没有关于如何处理这些问题的建议?绘制给定坐标非常容易。你所要做的就是申请一份工作 我复制了您的示例并添加了注释,其中我更改了某些内容以及更改原因
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'))
结果是:
请注意,如果您希望画布仅显示图像的区域,请稍后再次应用限制:
# Add a scatter point which is in the extend of the image:
plt.scatter(85.3, -2.5, color='red', transform=ax.get_transform('world'))
plt.ylim(0, img.shape[0])
plt.xlim(0, img.shape[1])
其中:
这里还有一个旁注。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'))
哇!非常感谢您的回复和所有伟大的建议!