我想在整个天空的草图上绘制 FITS 图像。
我在绘制整个天空时走到了这一步:
import matplotlib.pyplot as plt
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
image_data = fits.getdata(image_file, ext=0)
plt.subplot(111, projection='aitoff')
plt.grid(True)
plt.imshow(image_data, cmap='gray')
plt.show()
但我似乎无法让 FITS 图像与网格正确对齐
中的实际位置
最佳答案
image_data
只是一个像素值数组,实际上并不包含有关天空 map 上像素位置和方向的任何信息。您还必须从 FITS 文件中提取该信息。
import matplotlib.pyplot as plt
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
from astropy.wcs import WCS
from astropy.visualization.wcsaxes.frame import EllipticalFrame
image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')
image_header = fits.open(image_file)[0].header # extract header info
image_data = fits.getdata(image_file, ext=0)
# use the WCS class to get coordinate info and projection axes to use
wcs = WCS(image_header)
# make the axes using the EllipticalFrame class
ax = plt.subplot(projection=wcs, frame_class=EllipticalFrame)
# add the image
im = ax.imshow(image_data, origin='lower')
# add a grid
overlay = ax.get_coords_overlay('fk5')
overlay.grid(color='white', ls='dotted')
由于特定的图像示例没有延伸到整个天空,您只会看到一个小补丁(但您可以根据需要使用 ax.set_xlim()
和 ax.set_ylim()
扩展绘图,尽管我不确定单位是,同样值得注意的是,这张图片实际上只覆盖了一小片天空),但如果你的图片覆盖了整个天空,它看起来就像一个 Aitoff 投影,如图所示 here .
我认为这只适用于最新版本的 astropy (v3.1)、Matplotlib (v3.0.2),因此需要 Python >= 3.5。
您可能还想看看 healpy ,尽管无法在 astropy 示例 FITS 文件中读取。
关于python - 在整个天空的网格上绘制 FITS 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54707787/