In [9]:
#import urllib, tarfile
#url = 'http://python4astronomers.github.com/_downloads/ic348_wise.tar'
#tarfile.open(fileobj=urllib.request.urlopen(url), mode='r|').extractall()
In [1]:
%matplotlib inline

import matplotlib.pyplot as plt
import aplpy

create a new figure to plot the FITS file with:

In [2]:
# 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]

Color Images

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:

In [3]:
import montage_wrapper as montage
In [4]:
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]

Publication Quality Images

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

In [16]:
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)
In [6]:
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)