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')
/home/david/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py:221: MatplotlibDeprecationWarning: This has been deprecated in mpl 1.5, please use the axes property. A removal date has not been set. warnings.warn(_get_axes_msg, mplDeprecation, stacklevel=1) INFO:astropy:Auto-setting vmin to -2.338e+01 INFO:astropy:Auto-setting vmax to 3.247e+02
INFO: Auto-setting vmin to -2.338e+01 [aplpy.core] INFO: Auto-setting vmax to 3.247e+02 [aplpy.core] INFO
INFO:astropy:Auto-setting vmin to 5.383e+00 INFO:astropy:Auto-setting vmax to 2.930e+02
: Auto-setting vmin to 5.383e+00 [aplpy.core] INFO: Auto-setting vmax to 2.930e+02 [aplpy.core] INFO
INFO:astropy:Auto-setting resolution to 187.814 dpi
: Auto-setting resolution to 187.814 dpi [aplpy.core]
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')
INFO:astropy:Deleting work directory /tmp/tmpk6ftdqzb
INFO: Deleting work directory /tmp/tmpk6ftdqzb [montage_wrapper.wrappers] INFO
INFO:astropy:Deleting work directory /tmp/tmp01wcnx49
: Deleting work directory /tmp/tmp01wcnx49 [montage_wrapper.wrappers] INFO
INFO:astropy:Deleting work directory /tmp/tmp06ux7zru
: Deleting work directory /tmp/tmp06ux7zru [montage_wrapper.wrappers] INFO
INFO:astropy:Red:
: Red: [aplpy.rgb] INFO
INFO:astropy:vmin = 7.530e+02 (auto) INFO:astropy:vmax = 1.800e+03 (auto) INFO:astropy:Green:
: vmin = 7.530e+02 (auto) [aplpy.rgb] INFO: vmax = 1.800e+03 (auto) [aplpy.rgb] INFO: Green: [aplpy.rgb] INFO
INFO:astropy:vmin = 1.127e+01 (auto) INFO:astropy:vmax = 1.825e+02 (auto) INFO:astropy:Blue:
: vmin = 1.127e+01 (auto) [aplpy.rgb] INFO: vmax = 1.825e+02 (auto) [aplpy.rgb] INFO: Blue: [aplpy.rgb] INFO
INFO:astropy:vmin = 5.381e+00 (auto) INFO:astropy:vmax = 2.922e+02 (auto)
: vmin = 5.381e+00 (auto) [aplpy.rgb] INFO: vmax = 2.922e+02 (auto) [aplpy.rgb] INFO
/home/david/anaconda3/lib/python3.5/site-packages/aplpy/rgb.py:218: UserWarning: PyAVM 0.9.1 or later is not installed, so AVM tags will not be embedded in RGB image warnings.warn("PyAVM 0.9.1 or later is not installed, so AVM tags will not be embedded in RGB image") INFO:astropy:Red:
: Red: [aplpy.rgb] INFO
INFO:astropy:vmin = 7.530e+02 (auto) INFO:astropy:vmax = 1.800e+03 (auto)
: vmin = 7.530e+02 (auto) [aplpy.rgb] INFO: vmax = 1.800e+03 (auto) [aplpy.rgb] INFO
INFO:astropy:Green:
: Green: [aplpy.rgb] INFO
INFO:astropy:vmin = 1.127e+01 (auto) INFO:astropy:vmax = 1.825e+02 (auto)
: vmin = 1.127e+01 (auto) [aplpy.rgb] INFO: vmax = 1.825e+02 (auto) [aplpy.rgb] INFO
INFO:astropy:Blue:
: Blue: [aplpy.rgb] INFO
INFO:astropy:vmin = 5.381e+00 (auto) INFO:astropy:vmax = 2.922e+02 (auto)
: vmin = 5.381e+00 (auto) [aplpy.rgb] INFO: vmax = 2.922e+02 (auto) [aplpy.rgb] INFO
/home/david/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py:221: MatplotlibDeprecationWarning: This has been deprecated in mpl 1.5, please use the axes property. A removal date has not been set. warnings.warn(_get_axes_msg, mplDeprecation, stacklevel=1) INFO:astropy:Auto-setting resolution to 188.244 dpi
: Auto-setting resolution to 188.244 dpi [aplpy.core]
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')
/home/david/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py:221: MatplotlibDeprecationWarning: This has been deprecated in mpl 1.5, please use the axes property. A removal date has not been set. warnings.warn(_get_axes_msg, mplDeprecation, stacklevel=1) INFO:astropy:Auto-setting vmin to -5.899e+00 INFO:astropy:Auto-setting vmax to 2.019e+02
INFO: Auto-setting vmin to -5.899e+00 [aplpy.core] INFO: Auto-setting vmax to 2.019e+02 [aplpy.core] INFO
INFO:astropy:Auto-setting vmin to 1.127e+01 INFO:astropy:Auto-setting vmax to 1.830e+02
: Auto-setting vmin to 1.127e+01 [aplpy.core] INFO: Auto-setting vmax to 1.830e+02 [aplpy.core] Auto-detected table type: fits There is one layer in this figure: -> marker_set_1
/home/david/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py:221: MatplotlibDeprecationWarning: This has been deprecated in mpl 1.5, please use the axes property. A removal date has not been set. warnings.warn(_get_axes_msg, mplDeprecation, stacklevel=1)
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')
/home/david/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py:221: MatplotlibDeprecationWarning: This has been deprecated in mpl 1.5, please use the axes property. A removal date has not been set. warnings.warn(_get_axes_msg, mplDeprecation, stacklevel=1) INFO:astropy:Auto-setting vmin to -7.773e-01 INFO:astropy:Auto-setting vmax to 8.578e+00
INFO: Auto-setting vmin to -7.773e-01 [aplpy.core] INFO: Auto-setting vmax to 8.578e+00 [aplpy.core] Auto-detected table type: fits
/home/david/anaconda3/lib/python3.5/site-packages/aplpy/normalize.py:115: RuntimeWarning: invalid value encountered in less negative = result < 0. /home/david/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py:221: MatplotlibDeprecationWarning: This has been deprecated in mpl 1.5, please use the axes property. A removal date has not been set. warnings.warn(_get_axes_msg, mplDeprecation, stacklevel=1)
With APLpy we’re able to make subplots within a figure just like Matplotlib and we can pull in a DS9 region files. Unfortunately, there is a bug in the official pyregion release, so it’s suggested that you download the latest development version here http://github.com/leejjoon/pyregion/zipball/master. Use pip to install the package like the following.:
pip install pyregion/ --user --upgrade
conda install -c https://conda.anaconda.org/astropy pyregion
Now, let’s work on an example that shows both capabilities below:
import aplpy
from astropy.io import fits
import matplotlib.pyplot as mpl
fig = mpl.figure()
# Initiating first subplot
f1 = aplpy.FITSFigure('w4.fits', figure=fig)
f1.show_grayscale()
# Load a regions file into APLpy plot
f1.show_regions('bowshock.reg')
# Modify the tick labels for precision and format
f1.tick_labels.set_xformat('hhmmss')
f1.tick_labels.set_yformat('ddmm')
"""
# Another way of adding a subplot to the figure
d2 = fits.getdata('w4_bw1.fits')
f2 = fig.add_axes([0.3,0.3,0.11,0.15])
f2.imshow(d2, origin='lower left', cmap='jet')
#f2.imshow(d,cmap='jet')
f2.axes.get_xaxis().set_visible(False)
f2.axes.get_yaxis().set_visible(False)
"""
# Initiating second subplot
f2 = aplpy.FITSFigure('w4_bw1.fits', figure=fig,
subplot=[0.3,0.3,0.10,0.15])
f2.show_colorscale(cmap = 'jet')
# Hiding the ticks, tick labels (numbers) and axis labels (e.g. RA & Dec.)
f2.ticks.hide()
f2.tick_labels.hide()
f2.axis_labels.hide()
#"""
# Another way of adding a subplot to the figure
d3 = fits.getdata('w4_bw2.fits')
f3 = fig.add_axes([0.25,0.7,0.15,0.15])
f3.imshow(d3, origin='lower left', cmap='jet')
#f3.imshow(d,cmap='jet')
f3.axes.get_xaxis().set_visible(False)
f3.axes.get_yaxis().set_visible(False)
fig.canvas.draw()
fig.savefig('subplots.pdf', bbox_inches='tight')
/home/david/anaconda3/lib/python3.5/site-packages/matplotlib/artist.py:221: MatplotlibDeprecationWarning: This has been deprecated in mpl 1.5, please use the axes property. A removal date has not been set. warnings.warn(_get_axes_msg, mplDeprecation, stacklevel=1) INFO:astropy:Auto-setting vmin to 2.282e+02 INFO:astropy:Auto-setting vmax to 3.004e+02 WARNING: AstropyDeprecationWarning: The ascard function is deprecated and may be removed in a future version. Use the `.cards` attribute instead. [pyregion.wcs_helper] WARNING:astropy:AstropyDeprecationWarning: The ascard function is deprecated and may be removed in a future version. Use the `.cards` attribute instead. WARNING: AstropyDeprecationWarning: The CardList class has been deprecated; all its former functionality has been subsumed by the Header class, so CardList objects should not be directly created. See the PyFITS 3.1.0 CHANGELOG for more details. [astropy.io.fits.card] WARNING:astropy:AstropyDeprecationWarning: The CardList class has been deprecated; all its former functionality has been subsumed by the Header class, so CardList objects should not be directly created. See the PyFITS 3.1.0 CHANGELOG for more details. WARNING: AstropyDeprecationWarning: The ascard function is deprecated and may be removed in a future version. Use the `.cards` attribute instead. [pyregion.wcs_helper] WARNING:astropy:AstropyDeprecationWarning: The ascard function is deprecated and may be removed in a future version. Use the `.cards` attribute instead.
INFO: Auto-setting vmin to 2.282e+02 [aplpy.core] INFO: Auto-setting vmax to 3.004e+02 [aplpy.core] INFO
INFO:astropy:Auto-setting vmin to 2.297e+02 INFO:astropy:Auto-setting vmax to 4.242e+02
: Auto-setting vmin to 2.297e+02 [aplpy.core] INFO: Auto-setting vmax to 4.242e+02 [aplpy.core]