Tutorial taken from http://www.ster.kuleuven.be/~pieterd/python/html/plotting/aplpy.html
Start off by downloading this tar file: http://www.ster.kuleuven.be/~pieterd/python/html/_downloads/ic348_wise.tar
#import urllib, tarfile
#url = 'http://python4astronomers.github.com/_downloads/ic348_wise.tar'
#tarfile.open(fileobj=urllib.request.urlopen(url), mode='r|').extractall()
%matplotlib inline
import matplotlib.pyplot as plt
import aplpy
create a new figure to plot the FITS file with:
# Launch APLpy figure of image
img = aplpy.FITSFigure('w1.fits')
# Apply grayscale mapping of image
img.show_grayscale()
# Or apply a different stretch to the image
img.show_grayscale(stretch='arcsinh')
# Specifically specify lower and upper limit
img.show_grayscale(stretch='arcsinh', vmin=1, vmax=290)
# Display a grid and tweak the properties
img.show_grid()
# Modify the tick labels for precision and format
img.tick_labels.set_xformat('hhmm')
img.tick_labels.set_yformat('ddmm')
# Move the tick labels
img.tick_labels.set_xposition('top')
img.tick_labels.set_yposition('right')
img.save('ic348_basic.eps')
Now let’s do something a bit more advanced where we’ll make a color image from the fits files and plot some extra features on top of it. conda install -c https://conda.anaconda.org/astropy montage-wrapper If you cannot get Montage and python-montage installed, then you can use the already produced files to follow along with the exercise:
import montage_wrapper as montage
import aplpy
# Make RGB cube - images do not need not to have the same WCS
aplpy.make_rgb_cube(['w3.fits','w2.fits','w1.fits'], 'cube.fits')
# Make RGB image - you can specify color parameters here
aplpy.make_rgb_image('cube.fits','rgb_image_linear.png')
# Customize it
aplpy.make_rgb_image('cube.fits','rgb_image_arcsinh.png',
stretch_r='arcsinh', stretch_g='arcsinh',
stretch_b='arcsinh')
# Launch APLpy figure of 2D cube
img = aplpy.FITSFigure('cube_2d.fits')
img.show_rgb('rgb_image_arcsinh.png')
# Maybe we would like the arcsinh stretched image more?
#img.show_rgb('ic348_color_arcsinh.png')
# Modify the tick labels for precision and format
img.tick_labels.set_xformat('hhmmss')
img.tick_labels.set_yformat('ddmm')
# Let's add a scalebar to it
img.add_scalebar(5/60.)
img.scalebar.set_label('5 arcmin')
img.scalebar.set_color('white')
# We may want to lengthen the scalebar, move it to the top left,
# and apply a physical scale
img.scalebar.set_corner('top left')
img.scalebar.set_length(17/60.)
img.scalebar.set_label('1 parsec')
img.save('ic348_color_arcsinh.pdf')
We’re ready at this point to start making science-grade plots with APLpy with contours and markers: conda install -c https://conda.anaconda.org/auto atpy
import aplpy, atpy
# Launch APLpy figure of image
img = aplpy.FITSFigure('w2.fits')
# Apply grayscale mapping of image
img.show_grayscale()
# Or apply a different stretch to the image
img.show_grayscale(stretch='arcsinh')
# Specifically specify lower and upper limit
img.show_grayscale(stretch='arcsinh', vmin=10, vmax=180)
# Modify the tick labels for precision and format
img.tick_labels.set_xformat('hhmmss')
img.tick_labels.set_yformat('ddmm')
# ATpy to pull in sources
# If you want to see what columns are available - type t.describe()
t = atpy.Table('per_ysoc_c2d.fits')
# Now, let's display the young stellar objects (YSOs) in the region
# using c2d Spitzer data
img.show_markers(t.ra, t.dec)
# But let's make the markers bigger and triangles instead
# Before we can do that we need to list the layers
img.list_layers()
img.remove_layer('marker_set_1')
# Now we add the new markers
img.show_markers(t.ra, t.dec, marker='^', s=100)
# Let's add a contour from Av map of the region from COMPLETE Survey
#img.show_contour('per_extn2mass.fits', levels=[8,7,6,5,4], colors='orange')
img.show_contour('per_av_j2000.fits', levels=4, colors='orange')
img.save('ic348_marker_contour.png')
import aplpy
import atpy
# Exercise
img = aplpy.FITSFigure('per_av_j2000.fits')
# Identical to show_grayscale
img.show_colorscale()
# Recenter the image with a defined width of 5 degrees
img.recenter(54.51, 31.39, radius=2.5)
# Show the grid
img.show_grid()
# Define tick label style
img.tick_labels.set_xformat('hhmm')
img.tick_labels.set_yformat('ddmm')
img.show_colorbar()
# Read in table using atpy
t = atpy.Table('per_ysoc_c2d.fits')
# Plot markers
img.show_markers(t.ra, t.dec, facecolor='black', edgecolor='none', s=20)
img.save('ic348_av_markers.png')