Viewing and manipulating data from FITS tables


Lia Corrales

Learning Goals

  • TODO


table, file input/output, matplotlib, FITS image


Demonstrates the use of to download a data file, uses and astropy.table to open the file, uses matplotlib to visualize the data.


import numpy as np
from import fits
from astropy.table import Table
from matplotlib.colors import LogNorm

# Set up matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

The following line is needed to download the example FITS files used in this tutorial.


from import download_file

FITS files can often contain large amount of multi-dimensional data and tables.

In this particular example, I will open a FITS file from a Chandra observation of the Galactic Center. The file contains a list of events with x and y coordinates, energy, and various other pieces of information.


event_filename = download_file('',


Downloading [Done]

Opening the FITS file and viewing table contents

Since the file is big, I will open with memmap=True to prevent RAM storage issues.


hdu_list =, memmap=True)



Filename: /home/circleci/.astropy/cache/download/py3/26e9900d731d08997d99ada3973f4592
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU      30   ()
  1  EVENTS        1 BinTableHDU    890   483964R x 19C   [1D, 1I, 1I, 1J, 1I, 1I, 1I, 1I, 1E, 1E, 1E, 1E, 1J, 1J, 1E, 1J, 1I, 1I, 32X]
  2  GTI           3 BinTableHDU     28   1R x 2C   [1D, 1D]
  3  GTI           2 BinTableHDU     28   1R x 2C   [1D, 1D]
  4  GTI           1 BinTableHDU     28   1R x 2C   [1D, 1D]
  5  GTI           0 BinTableHDU     28   1R x 2C   [1D, 1D]
  6  GTI           6 BinTableHDU     28   1R x 2C   [1D, 1D]

I’m interested in reading EVENTS, which contains information about each X-ray photon that hit the detector.

To find out what information the table contains, I will print the column names.




    name = 'time'; format = '1D'; unit = 's'
    name = 'ccd_id'; format = '1I'
    name = 'node_id'; format = '1I'
    name = 'expno'; format = '1J'
    name = 'chipx'; format = '1I'; unit = 'pixel'; coord_type = 'CPCX'; coord_unit = 'mm'; coord_ref_point = 0.5; coord_ref_value = 0.0; coord_inc = 0.023987
    name = 'chipy'; format = '1I'; unit = 'pixel'; coord_type = 'CPCY'; coord_unit = 'mm'; coord_ref_point = 0.5; coord_ref_value = 0.0; coord_inc = 0.023987
    name = 'tdetx'; format = '1I'; unit = 'pixel'
    name = 'tdety'; format = '1I'; unit = 'pixel'
    name = 'detx'; format = '1E'; unit = 'pixel'; coord_type = 'LONG-TAN'; coord_unit = 'deg'; coord_ref_point = 4096.5; coord_ref_value = 0.0; coord_inc = 0.00013666666666667
    name = 'dety'; format = '1E'; unit = 'pixel'; coord_type = 'NPOL-TAN'; coord_unit = 'deg'; coord_ref_point = 4096.5; coord_ref_value = 0.0; coord_inc = 0.00013666666666667
    name = 'x'; format = '1E'; unit = 'pixel'; coord_type = 'RA---TAN'; coord_unit = 'deg'; coord_ref_point = 4096.5; coord_ref_value = 266.41519201128; coord_inc = -0.00013666666666667
    name = 'y'; format = '1E'; unit = 'pixel'; coord_type = 'DEC--TAN'; coord_unit = 'deg'; coord_ref_point = 4096.5; coord_ref_value = -29.012248288366; coord_inc = 0.00013666666666667
    name = 'pha'; format = '1J'; unit = 'adu'; null = 0
    name = 'pha_ro'; format = '1J'; unit = 'adu'; null = 0
    name = 'energy'; format = '1E'; unit = 'eV'
    name = 'pi'; format = '1J'; unit = 'chan'; null = 0
    name = 'fltgrade'; format = '1I'
    name = 'grade'; format = '1I'
    name = 'status'; format = '32X'

Now I’ll we’ll take this data and convert it into an astropy table. While it is possible to access FITS tables directly from the .data attribute, using Table tends to make a variety of common tasks more convenient.


evt_data = Table(hdu_list[1].data)

For example, a preview of the table is easily viewed by simply running a cell with the table as the last line:




Table length=483964
timeccd_idnode_idexpnochipxchipytdetxtdetydetxdetyxyphapha_roenergypifltgradegradestatus [32]
238623220.909358333689208512439815095.6414138.9954168.07235087.7723548353413874.715951164False .. False
238623220.90935833168437237489534984865.5674621.18263662.19684915.93366676292621.1938180642False .. False
238623220.90935833268719289484337804814.8354340.2543935.22074832.5523033287512119.01883183False .. False
238623220.90935833068103295483731644807.36434954.3853324.46444897.27548317733253.036422300False .. False
238623220.90935833168498314481835594788.9874560.32763713.63434832.7353612343914214.382974642False .. False
238623220.90935833368791469466338524635.45264268.0533985.84964645.935004381952.723913400False .. False
238623220.90935833368894839429339554266.6424165.32034044.54694267.6058357133267.533422400False .. False
238623220.90935833368857941419139184164.8154202.22563995.93534170.8189758043817.036626200False .. False
238623220.90935833368910959417339714146.99374149.3644046.33764146.91065764462252.729515500False .. False
238623220.90935833368961962417040224144.12844098.49764096.5154138.09157213546154.109442200False .. False
238672393.549719331315723933199493350404902.9073082.49565212.49954766.2295122211814819.828633100False .. False
238672393.549719331215723596412472047034691.513418.98934853.51174595.80373142302012536.866859106False .. False
238672393.5497193313157231000608452451074494.7133015.71855230.8864353.0186585852599.565217900False .. False
238672393.549719331115723270917421543774188.33253743.59574472.074134.2213861346315535.7681024164False .. False
238672393.549719331015723232988414443394117.61473781.87744425.754068.4873168014996653.081545600False .. False
238672393.590759340115723366103316447663140.90483356.32084733.68163048.56643621360214362.48298400False .. False
238672393.590759340315723937646370741953681.21223925.54524231.83543651.97243717348614653.954100483False .. False
238672393.590759340115723406687374847263723.40143396.2524762.4213631.7224167615366652.82745600False .. False
238672393.590759340115723354870393147783906.073344.7754834.993807.0835243621659672.882663164False .. False
238672393.631799346115723384821325925233230.92045596.84962519.22023401.03274913561875.935912900False .. False

We can extract data from the table by referencing the column name.. For example, I’ll make a histogram for the energy of each photon, giving us a sense for the spectrum (folded with the detector’s efficiency).


energy_hist = plt.hist(evt_data['energy'], bins='auto')



Making a 2-D histogram with some table data

I will make an image by binning the x and y coordinates of the events into a 2-D histogram.

This particular observation spans five CCD chips. First we determine the events that only fell on the main (ACIS-I) chips, which have number ids 0, 1, 2, and 3.


ii = np.in1d(evt_data['ccd_id'], [0, 1, 2, 3])



Method 1: Use numpy to make a 2-D histogram and imshow to display it

This method allowed me to create an image without stretching


NBINS = (100,100)

img_zero, yedges, xedges = np.histogram2d(evt_data['x'][ii], evt_data['y'][ii], NBINS)

extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

plt.imshow(img_zero, extent=extent, interpolation='nearest', cmap='gist_yarg', origin='lower')


# To see more color maps


<matplotlib.text.Text at 0x7f38f1ca4898>

Method 2: Use hist2d with a log-normal color scheme


NBINS = (100,100)
img_zero_mpl = plt.hist2d(evt_data['x'][ii], evt_data['y'][ii], NBINS,
                          cmap='viridis', norm=LogNorm())

cbar = plt.colorbar(ticks=[1.0,3.0,6.0])['1','3','6'])



<matplotlib.text.Text at 0x7f38f1b9fc18>

Close the FITS file

When you’re done using a FITS file, it’s often a good idea to close it. That way you can be sure it won’t continue using up excess memory or file handles on your computer. (This happens automatically when you close Python, but you never know how long that might be…)




Make a scatter plot of the same data you histogrammed above. The plt.scatter function is your friend for this. What are the pros and cons of doing this?


Try the same with the plt.hexbin plotting function. Which do you think looks better for this kind of data?


Choose an energy range to make a slice of the FITS table, then plot it. How does the image change with different energy ranges?