Debian Codegraph @

docs/fitsfigure/howto_subplot.rst

raw
1 Creating subplots
2 -----------------
3
4 By default, :class:`~aplpy.FITSFigure` creates a figure with a single
5 subplot that occupies the entire figure. However, APLpy can be used to place a
6 subplot in an existing matplotlib figure instance. To do this,
7 :class:`~aplpy.FITSFigure` should be called with the ``figure=``
8 argument as follows::
9
10 import aplpy
11 import matplotlib.pyplot as mpl
12
13 fig = mpl.figure()
14 f = aplpy.FITSFigure('some_image.fits', figure=fig)
15
16 The above will place a subplot inside the ``fig`` figure instance. The ``f``
17 object can be used as normal to control the FITS figure inside the
18 subplot. The above however is not very interesting compared to just
19 creating a FITSFigure instance from scratch. What this is useful for is
20 only using sub-regions of the figure to display the FITS data, to leave
21 place for other subplots, whether histograms, scatter, or other matplotlib
22 plots, or another FITS Figure. This can be done using the ``subplot``
23 argument. From the docstring for FITSFigure::
24
25 *subplot*: [ list of four floats ]
26 If specified, a subplot will be added at this position. The list
27 should contain [xmin, ymin, dx, dy] where xmin and ymin are the
28 position of the bottom left corner of the subplot, and dx and dy are
29 the width and height of the subplot respectively. These should all be
30 given in units of the figure width and height. For example, [0.1, 0.1,
31 0.8, 0.8] will almost fill the entire figure, leaving a 10 percent
32 margin on all sides.
33
34 The following code outline illustrates how to create a rectangular figure with
35 two FITS images::
36
37 import aplpy
38 import matplotlib.pyplot as mpl
39
40 fig = mpl.figure(figsize=(15, 7))
41
42 f1 = aplpy.FITSFigure('image_1.fits', figure=fig, subplot=[0.1,0.1,0.35,0.8])
43 f1.set_tick_labels_font(size='x-small')
44 f1.set_axis_labels_font(size='small')
45 f1.show_grayscale()
46
47 f2 = aplpy.FITSFigure('image_2.fits', figure=fig, subplot=[0.5,0.1,0.35,0.8])
48 f2.set_tick_labels_font(size='x-small')
49 f2.set_axis_labels_font(size='small')
50 f2.show_grayscale()
51
52 f2.hide_yaxis_label()
53 f2.hide_ytick_labels()
54
55 fig.canvas.draw()
56
57 The ``hide`` methods shown above are especially useful when working with
58 subplots, as in some cases there is no need to repeat the tick labels. Alternatively figures can be constructed from both APLpy figures and normal matplotlib axes::
59
60 import aplpy
61 import matplotlib.pyplot as mpl
62
63 fig = mpl.figure(figsize=(15, 7))
64
65 f1 = aplpy.FITSFigure('image_1.fits', figure=fig, subplot=[0.1,0.1,0.35,0.8])
66 f1.set_tick_labels_font(size='x-small')
67 f1.set_axis_labels_font(size='small')
68 f1.show_grayscale()
69
70 ax2 = fig.add_axes([0.5,0.1,0.35,0.8])
71
72 # some code here with ax2
73
74 fig.canvas.draw()