从FITS文件读取并打印图像

此示例打开存储在FITS文件中的图像并将其显示在屏幕上。

此示例使用 astropy.utils.data 要下载文件, astropy.io.fits 打开文件,然后 matplotlib.pyplot 显示图像。

By: Lia R. Corrales, Adrian Price-Whelan, Kelle Cruz

许可证:BSD

设置matplotlib并使用一组更好的绘图参数

import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)

下载本例使用的示例FITS文件:

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

使用 astropy.io.fits.info() 要显示文件的结构:

fits.info(image_file)
Filename: /home/bk/.astropy/cache/download/url/ff6e0b93871033c68022ca026a956d87/contents
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU     161   (891, 893)   int16
  1  er.mask       1 TableHDU        25   1600R x 4C   [F6.2, F6.2, F6.2, F6.2]

通常,图像信息位于主HDU中,也称为扩展0。在这里,我们使用 astropy.io.fits.getdata() 使用keyword参数从第一个扩展读取图像数据 ext=0

image_data = fits.getdata(image_file, ext=0)

数据现在存储为2D numpy数组。使用形状属性打印尺寸:

print(image_data.shape)
(893, 891)

显示图像数据:

plt.figure()
plt.imshow(image_data, cmap='gray')
plt.colorbar()
plot fits image
<matplotlib.colorbar.Colorbar object at 0x7fa31b74e430>

Total running time of the script: (0分3.088秒)

Gallery generated by Sphinx-Gallery