.. _sphx_glr_auto_examples_io_plot_fits-image.py: ======================================= 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 dispay the image. ------------------- *By: Lia R. Corrales, Adrian Price-Whelan, Kelle Cruz* *License: BSD* ------------------- Set up matplotlib and use a nicer set of plot parameters .. code-block:: python 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: .. code-block:: python from astropy.utils.data import download_file from astropy.io import fits image_file = download_file('http://data.astropy.org/tutorials/FITS-images/HorseHead.fits', cache=True) .. rst-class:: sphx-glr-script-out Out:: Downloading http://data.astropy.org/tutorials/FITS-images/HorseHead.fits [Done] Use `astropy.io.fits.info()` to display the structure of the file: .. code-block:: python fits.info(image_file) .. rst-class:: sphx-glr-script-out Out:: Filename: /home/docs/.astropy/cache/download/py2/2c9202ae878ecfcb60878ceb63837f5f No. Name Type Cards Dimensions Format 0 PRIMARY PrimaryHDU 161 (891, 893) int16 1 er.mask 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``: .. code-block:: python 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: .. code-block:: python print(image_data.shape) .. rst-class:: sphx-glr-script-out Out:: (893, 891) Display the image data: .. code-block:: python plt.figure() plt.imshow(image_data, cmap='gray') plt.colorbar() .. image:: /auto_examples/io/images/sphx_glr_plot_fits-image_001.png :align: center **Total running time of the script:** (0 minutes 0.397 seconds) .. container:: sphx-glr-download **Download Python source code:** :download:`plot_fits-image.py ` .. container:: sphx-glr-download **Download IPython notebook:** :download:`plot_fits-image.ipynb `