Creating figures from FITS images with APLpy

The Python module APLpy is a great tool for plotting professional-looking astro images.

APLpy example plot

Short for Astronomical Plotting Library in Python, APLpy aims to produce publication-quality plots of astronomical images in FITS file format. As I’ve been using it a lot recently, I thought I’d share some of my code to demonstrate the types of figures you can generate.

To start, you’ll need to already several python modules installed. Matplotlib, Numpy, Astropy are required, but I’d also recommend Pyregion and Pyparsing. Once these are on your system, you can download and install APLpy.

For this example, I’m going to be using some FITS images of a nearby galaxy, NGC 891, taken from the public archives of:

Digitized Sky Survey (DSS),
Wide-field Infrared Survey Explorer (WISE), and the
Galaxy Evolution Explorer (GALEX).

To experiment with APLpy, you can download the full script described below ( as well tar of the input files (ngc891_example_files.tar.gz) used in the script.

Here’s how to create the above figure.

Let’s start looking at the example Python script for plotting a FITS image as a sky map with R.A. and Dec. axis labels. First, we need to import the required modules.

[code language=”python”]
import aplpy
import pyparsing
import pyregion
import pyfits
import matplotlib.pyplot as pyplot
import matplotlib as mpl

The next part of the code creates the figure and sets the size along with many more optional properties. For comparing multiwavelength observations by plotting them side-by-side in subplots, it’s often useful to define a set of standard properties, such as axis and tick labels. The axis labels for longitude and lattitude can take a number of formats; here I use the sexagesimal notation (hh:mm:ss, dd:mm:ss). The image is centered on x,y (in decimal degrees). One handy feature to include is the use of LaTeX on the axis labels, allowing the full range of LaTeX math symbols on your plots.

[code language=”python”]
fig = pyplot.figure(figsize=(12, 5))

def standard_setup(sp):

The figure shown above is then reproduced using the APLpy FITSfigure class to read a Fits image into a subplot, and then calling the standard_setup() to create the map.

[code language=”python”]
f1 = aplpy.FITSFigure(‘ngc891_dssb.fits’, figure=fig,

Adding another panel, for e.g. directly comparing observations at different wavelengths, is simple a matter of creating a new subplot exactly as above, but shifting the subplot coordinates.

[code language=”python”]
f2 = aplpy.FITSFigure(‘ngc891_galexnuv.fits’, figure=fig,

APLpy example plot

For one last panel, I’ll plot radio contours onto an infrared image. In this example, I’ve used a different colour map for the scale, and added a colourbar to the image.

[code language=”python”]
# Add third subplot
f3 = aplpy.FITSFigure(‘ngc891_wise4.fits’, figure=fig,
vmin=0.03,vmax =0.3, vmid=0.005)
f3.show_contour(‘ngc891_radio.fits’, colors=”white”, levels=14)

# Add a colourbar
axisf3 = fig.add_axes([0.92,0.19,0.02,0.68])
normf3 = mpl.colors.Normalize(vmin=0.03, vmax=0.3)
cbf3 = mpl.colorbar.ColorbarBase(axisf3,,
norm=normf3, orientation=’vertical’)

APLpy example plot

The code segment above produces this nice (but not really scientifically useful) subplot.

The colourbar can be placed anywhere on the plot by adjusting the add_axes() properties and the orientation keyword. The colour scale can be set as linear or logarithmic and with different normalisations. The documentation describes all the different formatting options.

I’ve also added some contours taken from data in another FITS image using show_contours(). Again, there are lots of formatting parameters that can be tweaked to easily produce excellent plots.

Finally, we can add some finishing touches to the plot and save our work.

The following code segment adds text labels to the figure…

[code language=”python”]
fig.text(0.20,0.89,”DSS B Band”,color=’white’,size=’12’)
fig.text(0.515,0.89,”GALEX NUV”,color=’white’,size=’12’)
fig.text(0.83,0.89,”WISE 22 $\mu$m”,color=’white’,size=’12’)

… and we can save the final images in a variety of formats.

[code language=”python”]
fig.savefig(“ngc891_figure.eps”, dpi=600)
fig.savefig(“ngc891_figure.png”, dpi=300)

All the code together produces the figure below.

I hope you find this example useful. Feel free to ask any questions in the comments section!

APLpy example plot

Posted in Code | Tagged , , , | Leave a comment

Leave a Reply

Your email address will not be published.