Read and plot an image from a FITS file

This example opens an image stored in a FITS file and displays it to the screen.

This example uses to download the file, to open the file, and matplotlib.pyplot to display the image.

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

License: BSD

Set up matplotlib and use a nicer set of plot parameters

import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style

Download the example FITS files used by this example:

from import get_pkg_data_filename
from import fits

image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')

Use to display the structure of the file:


Filename: /home/docs/.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]

Generally the image information is located in the Primary HDU, also known as extension 0. Here, we use to read the image data from this first extension using the keyword argument ext=0:

The data is now stored as a 2D numpy array. Print the dimensions using the shape attribute:


(893, 891)

Display the image data:

plot fits image


/home/docs/checkouts/ MatplotlibDeprecationWarning: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first.

<matplotlib.colorbar.Colorbar object at 0x7f6546dd17f0>

Total running time of the script: ( 0 minutes 0.474 seconds)

Gallery generated by Sphinx-Gallery