python - 在整个天空的网格上绘制 FITS 图像

标签 python python-3.x matplotlib astropy

我想在整个天空的草图上绘制 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 图像与网格正确对齐


上面的代码结果如下 actual result 但我想得到更像 expected result 其中蓝色方 block 是图像在 image_file

中的实际位置

最佳答案

image_data只是一个像素值数组,实际上并不包含有关天空 map 上像素位置和方向的任何信息。您还必须从 FITS 文件中提取该信息。

一个例子是(基于来自 herehere 的信息):

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')

plot of horsehead nebula

由于特定的图像示例没有延伸到整个天空,您只会看到一个小补丁(但您可以根据需要使用 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/

相关文章:

html - WTForm BooleanField 和默认值

python - 只有一个线程正在启​​动 python

python - pyplot twinx() 刻度重叠

python - 如何解析 Python3 BeautifulSoup 中的 onclick() 文本?

python - matplotlib savefig 不输出文件

python - 无法让 Canvas 在 matplotlib 中绘制无花果

python - 在 tkinter 中仅选择文本而不是整行

python - Django查询集在查询中获取空值

python - python中函数之间的通信

python - 使用Polars处理多个csv文件并对它们进行不同的分区