APLpy (the Astronomical Plotting Library in Python) is a Python module aimed at producing publication-quality plots of astronomical imaging data in FITS format. It effectively provides a layer on top of Matplotlib to enable plotting of Astronomical images, and allows users to:
The APLpy Documentation contains all the information needed to run APLpy successfully. The most important page is the Quick Reference Guide which provides concise instructions for all of the APLpy functions. In particular, note that the interface to APLpy is slightly different to Matplotlib, so not all Matplotlib commands will work in APLpy. Be sure to check out the quick reference guide!
Start off by downloading this tar file, expand it, and go to the APLpy-example directory on the command line. Then, launch pylab:
$ ipython --pylab
Import the aplpy module (note the lowercase module name):
And create a new figure to plot the FITS file with:
f = aplpy.FITSFigure('ngc2264_cutout.fits')
This should open up a matplotlib window and will show an empty set of axes with coordinates. From now on, you will interact with the figure by calling methods associated with f. For example, show the image as a grayscale:
The automatic settings for the stretch should be decent, but there are of course options to allow custom min/max levels. You can now try panning around and zooming in like you would do in Matplotlib, and you will notice the coordinates updating. Press the Home button to reset the view.
Next, let’s overlay a set of contours from a different image:
We can also add a coordinate grid:
And finally let’s add a scalebar to make the plot look sciency:
f.add_scalebar(0.03, '0.5pc', color='white')
We can now save our masterpiece either by clicking on the Save icon in the matplotlib window, or doing:
The latter is recommended because it will automatically figure out the best resolution with which to output your plot. Your plot should look something like this:
Use the help or ? functionality in ipython to figure out how to set the min/max levels on the grayscale manually, and to change the stretch function to a square-root stretch. Also use the Use the Quick Reference Guide to figure out how to change the grayscale to a colorscale.
Click to Show/Hide Solution
To manually set the levels:
To additionally use a square-root stretch:
To change to a colorscale:
Note that the colormap can be set using for example:
where the value of the cmap argument can be any of the names listed on this page.
Use the Quick Reference Guide to manually set the tick spacing on both axes. In the default view for the example FITS file above, the arcseconds in the declination are not useful (they are always zero). Try and change the format of the y-axis labels so that they only include degrees and arcminutes.
Click to Show/Hide Solution
To set the tick spacing:
To show the y-axis labels in dd:mm format:
Use APLpy to plot one of your own FITS images! If you don’t have any FITS files at hand, you can play with this newly-released WISE data of M82!
If you have trouble downloading the file, then start up IPython (ipython -pylab) and enter:
import urllib2, tarfile url = 'http://python4astronomers.github.com/_downloads/m82_wise.tar' tarfile.open(fileobj=urllib2.urlopen(url), mode='r|').extractall() cd m82_wise ls