[ ]:
%matplotlib inline

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 astropy.utils.data to download the file, astropy.io.fits 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

[7]:
import matplotlib.pyplot as plt
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)

Download the example FITS files used by this example:

[18]:
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
image_file = get_pkg_data_filename(r'C:\Users\Kurt\Documents\Notebooks\astropy examples\data\dss_search.Rigel.bet.Ori.BlueSG.fits')

Image of star Rigel o.t. Orion constellation, a blue super giant.

Use astropy.io.fits.info() to display the structure of the file:

[3]:
fits.info(image_file)
Filename: C:\Users\Kurt\Documents\Notebooks\astropy examples\data\dss_search.Rigel.bet.Ori.BlueSG.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU     161   (883, 883)   int16
  1  s.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 astropy.io.fits.getdata() to read the image data from this first extension using the keyword argument ext=0:

[4]:
image_data = fits.getdata(image_file, ext=0)

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

[8]:
print(image_data.shape)
(883, 883)

Display the image data:

[9]:
plt.figure(figsize=(15,15))
plt.imshow(image_data, cmap='plasma')
plt.colorbar()
[9]:
<matplotlib.colorbar.Colorbar at 0x13042e60e08>
_images/plot_fits-image_15_1.png
[17]:
from IPython.display import Image
i = Image(filename=r'C:\Users\Kurt\Documents\Notebooks\astropy examples\doc-example\source\_static\rigel.jpg', embed=True, unconfined=True)
i
[17]:
_images/plot_fits-image_16_0.jpg

Image of the bright star Rigel in the Orion constellation.

[ ]: