| 1 |
APLpy in non-interactive mode |
| 2 |
----------------------------- |
| 3 |
|
| 4 |
While APLpy can be easily used to interactively make plots, it is also |
| 5 |
possible to make plots without opening up a display. This can be |
| 6 |
useful to run APLpy remotely, or to write non-interactive scripts to |
| 7 |
plot one or many FITS files. |
| 8 |
|
| 9 |
Running APLpy in non-interactive mode |
| 10 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 11 |
|
| 12 |
To run APLpy in non-interactive mode, you will need to change the |
| 13 |
'backend' used by matplotlib from an interactive (e.g. WxAgg, TkAgg, |
| 14 |
MacOS X) to a non-interactive (e.g. Agg, Cairo, PS, PDF) backend. The |
| 15 |
default backend is typically set in your ``.matplotlibrc`` |
| 16 |
file (or if you do not have such a file, an interactive backend is |
| 17 |
usually chosen by default). The easiest way to change the backend |
| 18 |
temporarily is to use the ``matplotlib.use()`` function:: |
| 19 |
|
| 20 |
import matplotlib |
| 21 |
matplotlib.use('Agg') |
| 22 |
|
| 23 |
It is important to change the backend via ``matplotlib.use`` |
| 24 |
before the ``aplpy`` module is imported. Once the backend has |
| 25 |
been changed, any call to ``FITSFigure()`` will no longer make a figure |
| 26 |
window appear. The following script can be used to make a PNG plot:: |
| 27 |
|
| 28 |
import matplotlib |
| 29 |
matplotlib.use('Agg') |
| 30 |
|
| 31 |
import aplpy |
| 32 |
|
| 33 |
f = aplpy.FITSFigure('mips_24micron.fits') |
| 34 |
f.show_grayscale() |
| 35 |
f.save('mips_24.png') |
| 36 |
|
| 37 |
Pan/Zoom in non-interactive mode |
| 38 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 39 |
|
| 40 |
One of the advantages of using APLpy in interactive mode is the |
| 41 |
ability to zoom in on a given region of interest. To replicate this |
| 42 |
functionality in non-interactive mode, the |
| 43 |
``FITSFigure.recenter()`` method can be used. This method |
| 44 |
takes a central position, and either a radius (to make a square plot) |
| 45 |
or a width/height (to make a rectangular plot). This is illustrated in |
| 46 |
the following example:: |
| 47 |
|
| 48 |
import matplotlib |
| 49 |
matplotlib.use('Agg') |
| 50 |
|
| 51 |
import aplpy |
| 52 |
|
| 53 |
f = aplpy.FITSFigure('mips_24micron.fits') |
| 54 |
f.show_grayscale() |
| 55 |
f.recenter(266.2142,-29.1832,width=0.5,height=0.3) |
| 56 |
f.save('mips_24_zoomin.png') |
| 57 |
|
| 58 |
Batch scripting |
| 59 |
^^^^^^^^^^^^^^^ |
| 60 |
|
| 61 |
Using APLpy non-interactively can be useful for making plots of many |
| 62 |
FITS files. Given a directory ``fits/`` containing FITS |
| 63 |
files, the following code will generate on plot for each |
| 64 |
``.fits`` file:: |
| 65 |
|
| 66 |
import matplotlib |
| 67 |
matplotlib.use('Agg') |
| 68 |
|
| 69 |
import aplpy |
| 70 |
|
| 71 |
import glob |
| 72 |
import os |
| 73 |
|
| 74 |
for fits_file in glob.glob(os.path.join('fits/','*.fits')): |
| 75 |
|
| 76 |
f = aplpy.FITSFigure(fits_file) |
| 77 |
f.show_grayscale() |
| 78 |
f.save(fits_file.replace('.fits','.png')) |
| 79 |
f.close() |
| 80 |
|
| 81 |
|