.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/examples/io/plot_fits-image.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_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 display the image. *By: Lia R. Corrales, Adrian Price-Whelan, Kelle Cruz* *License: BSD* .. GENERATED FROM PYTHON SOURCE LINES 20-21 Set up matplotlib and use a nicer set of plot parameters .. GENERATED FROM PYTHON SOURCE LINES 21-28 .. code-block:: Python import matplotlib.pyplot as plt from astropy.visualization import astropy_mpl_style plt.style.use(astropy_mpl_style) .. GENERATED FROM PYTHON SOURCE LINES 29-30 Download the example FITS files used by this example: .. GENERATED FROM PYTHON SOURCE LINES 30-36 .. code-block:: Python from astropy.io import fits from astropy.utils.data import get_pkg_data_filename image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits') .. GENERATED FROM PYTHON SOURCE LINES 37-38 Use `astropy.io.fits.info()` to display the structure of the file: .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python fits.info(image_file) .. rst-class:: sphx-glr-script-out .. code-block:: none Filename: /home/user/.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] .. GENERATED FROM PYTHON SOURCE LINES 42-45 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``: .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python image_data = fits.getdata(image_file, ext=0) .. GENERATED FROM PYTHON SOURCE LINES 49-51 The data is now stored as a 2D numpy array. Print the dimensions using the shape attribute: .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: Python print(image_data.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none (893, 891) .. GENERATED FROM PYTHON SOURCE LINES 55-56 Display the image data: .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: Python plt.figure() plt.imshow(image_data, cmap='gray') plt.colorbar() .. image-sg:: /generated/examples/io/images/sphx_glr_plot_fits-image_001.png :alt: plot fits image :srcset: /generated/examples/io/images/sphx_glr_plot_fits-image_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.867 seconds) .. _sphx_glr_download_generated_examples_io_plot_fits-image.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_fits-image.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_fits-image.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_