|
2 | 2 | Making subplots |
3 | 3 | =============== |
4 | 4 |
|
5 | | -When you're preparing a figure for a paper, there will often be times when |
6 | | -you'll need to put many individual plots into one large figure, and label them |
7 | | -'abcd'. These individual plots are called subplots. |
| 5 | +When you're preparing a figure for a paper, there will often be times when you'll need |
| 6 | +to put many individual plots into one large figure, and tag them 'abcd'. These |
| 7 | +individual plots are called subplots. |
8 | 8 |
|
9 | 9 | There are two main ways to create subplots in GMT: |
10 | 10 |
|
11 | | -- Use :meth:`pygmt.Figure.shift_origin` to manually move each individual plot |
12 | | - to the right position. |
| 11 | +- Use :meth:`pygmt.Figure.shift_origin` to manually move each individual plot to the |
| 12 | + right position. |
13 | 13 | - Use :meth:`pygmt.Figure.subplot` to define the layout of the subplots. |
14 | 14 |
|
15 | | -The first method is easier to use and should handle simple cases involving a |
16 | | -couple of subplots. For more advanced subplot layouts, however, we recommend |
17 | | -the use of :meth:`pygmt.Figure.subplot` which offers finer grained control, and |
18 | | -this is what the tutorial below will cover. |
| 15 | +The first method is easier to use and should handle simple cases involving a couple of |
| 16 | +subplots. For more advanced subplot layouts, however, we recommend the use of |
| 17 | +:meth:`pygmt.Figure.subplot` which offers finer grained control, and this is what the |
| 18 | +tutorial below will cover. |
19 | 19 | """ |
20 | 20 |
|
21 | 21 | # %% |
|
31 | 31 | # Define subplot layout |
32 | 32 | # --------------------- |
33 | 33 | # |
34 | | -# The :meth:`pygmt.Figure.subplot` method is used to set up the layout, size, |
35 | | -# and other attributes of the figure. It divides the whole canvas into regular |
36 | | -# grid areas with *n* rows and *m* columns. Each grid area can contain an |
37 | | -# individual subplot. For example: |
| 34 | +# The :meth:`pygmt.Figure.subplot` method is used to set up the layout, size, and other |
| 35 | +# attributes of the figure. It divides the whole canvas into regular grid areas with |
| 36 | +# *n* rows and *m* columns. Each grid area can contain an individual subplot. For |
| 37 | +# example: |
38 | 38 |
|
39 | 39 | # %% |
40 | 40 | # .. code-block:: default |
|
44 | 44 |
|
45 | 45 | # %% |
46 | 46 | # will define our figure to have a 2 row and 3 column grid layout. |
47 | | -# ``figsize=("15c", "6c")`` defines the overall size of the figure to be 15 cm |
48 | | -# wide by 6 cm high. Using ``frame="lrtb"`` allows us to customize the map |
49 | | -# frame for all subplots instead of setting them individually. The figure |
50 | | -# layout will look like the following: |
| 47 | +# ``figsize=("15c", "6c")`` defines the overall size of the figure to be 15 cm wide by |
| 48 | +# 6 cm high. Using ``frame="lrtb"`` allows us to customize the map frame for all |
| 49 | +# subplots instead of setting them individually. The figure layout will look like the |
| 50 | +# following: |
51 | 51 |
|
52 | 52 | with fig.subplot(nrows=2, ncols=3, figsize=("15c", "6c"), frame="lrtb"): |
53 | 53 | for i in range(2): # row number starting from 0 |
|
62 | 62 | fig.show() |
63 | 63 |
|
64 | 64 | # %% |
65 | | -# The :meth:`pygmt.Figure.set_panel` method activates a specified subplot, |
66 | | -# and all subsequent plotting methods will take place in that subplot panel. |
67 | | -# This is similar to matplotlib's ``plt.sca`` method. In order to specify a |
68 | | -# subplot, you will need to provide the identifier for that subplot via the |
69 | | -# ``panel`` parameter. Pass in either the *index* number, or a tuple/list like |
70 | | -# (*row*, *col*) to ``panel``. |
| 65 | +# The :meth:`pygmt.Figure.set_panel` method activates a specified subplot, and all |
| 66 | +# subsequent plotting methods will take place in that subplot panel. This is similar to |
| 67 | +# matplotlib's ``plt.sca`` method. In order to specify a subplot, you will need to |
| 68 | +# provide the identifier for that subplot via the ``panel`` parameter. Pass in either |
| 69 | +# the *index* number, or a tuple/list like (*row*, *col*) to ``panel``. |
71 | 70 |
|
72 | 71 | # %% |
73 | 72 | # .. note:: |
74 | 73 | # |
75 | | -# The row and column numbering starts from 0. So for a subplot layout with |
76 | | -# N rows and M columns, row numbers will go from 0 to N-1, and column |
77 | | -# numbers will go from 0 to M-1. |
| 74 | +# The row and column numbering starts from 0. So for a subplot layout with N rows |
| 75 | +# and M columns, row numbers will go from 0 to N-1, and column numbers will go from |
| 76 | +# 0 to M-1. |
78 | 77 |
|
79 | 78 | # %% |
80 | | -# For example, to activate the subplot on the top right corner (index: 2) at |
81 | | -# *row*\=0 and *col*\=2, so that all subsequent plotting commands happen |
82 | | -# there, you can use the following command: |
| 79 | +# For example, to activate the subplot on the top right corner (index: 2) at *row*\=0 |
| 80 | +# and *col*\=2, so that all subsequent plotting commands happen there, you can use the |
| 81 | +# following command: |
83 | 82 |
|
84 | 83 | # %% |
85 | 84 | # .. code-block:: default |
86 | 85 | # |
87 | | -# with fig.set_panel(panel=[0, 2]): |
| 86 | +# with fig.set_panel(panel=(0, 2)): |
88 | 87 | # ... |
89 | 88 |
|
90 | 89 |
|
91 | 90 | # %% |
92 | 91 | # Making your first subplot |
93 | 92 | # ------------------------- |
94 | 93 | # |
95 | | -# Next, let's use what we learned above to make a 2 row by 2 column subplot |
96 | | -# figure. We'll also pick up on some new parameters to configure our subplot. |
| 94 | +# Next, let's use what we learned above to make a 2 row by 2 column subplot figure. |
| 95 | +# We'll also pick up on some new parameters to configure our subplot. |
97 | 96 |
|
98 | 97 | fig = pygmt.Figure() |
99 | 98 | with fig.subplot( |
100 | 99 | nrows=2, |
101 | 100 | ncols=2, |
102 | 101 | figsize=("15c", "6c"), |
103 | | - autolabel=True, |
| 102 | + tag=True, |
104 | 103 | frame=["af", "WSne"], |
105 | 104 | margins=["0.1c", "0.2c"], |
106 | 105 | title="My Subplot Heading", |
|
113 | 112 |
|
114 | 113 | # %% |
115 | 114 | # In this example, we define a 2-row, 2-column (2x2) subplot layout using |
116 | | -# :meth:`pygmt.Figure.subplot`. The overall figure dimensions is set to be |
117 | | -# 15 cm wide and 6 cm high (``figsize=["15c", "6c"]``). In addition, we use |
118 | | -# some optional parameters to fine-tune some details of the figure creation: |
| 115 | +# :meth:`pygmt.Figure.subplot`. The overall figure dimensions is set to be 15 cm wide |
| 116 | +# and 6 cm high (``figsize=("15c", "6c")``). In addition, we use some optional |
| 117 | +# parameters to fine-tune some details of the figure creation: |
119 | 118 | # |
120 | | -# - ``autolabel=True``: Each subplot is automatically labelled 'abcd'. |
121 | | -# - ``margins=["0.1c", "0.2c"]``: Adjusts the space between adjacent subplots. |
122 | | -# In this case, it is set as 0.1 cm in the x-direction and 0.2 cm in the |
123 | | -# y-direction. |
| 119 | +# - ``tag=True``: Each subplot is automatically tagged 'abcd'. |
| 120 | +# - ``margins=["0.1c", "0.2c"]``: Adjusts the space between adjacent subplots. In this |
| 121 | +# case, it is set as 0.1 cm in the x-direction and 0.2 cm in the y-direction. |
124 | 122 | # - ``title="My Subplot Heading"``: Adds a title on top of the whole figure. |
125 | 123 | # |
126 | | -# Notice that each subplot was set to use a linear projection ``"X?"``. |
127 | | -# Usually, we need to specify the width and height of the map frame, but it is |
128 | | -# also possible to use a question mark ``"?"`` to let GMT decide automatically |
129 | | -# on what is the most appropriate width/height for each subplot's map frame. |
| 124 | +# Notice that each subplot was set to use a linear projection ``"X?"``. Usually, we need |
| 125 | +# to specify the width and height of the map frame, but it is also possible to use a |
| 126 | +# question mark ``"?"`` to let GMT decide automatically on what is the most appropriate |
| 127 | +# width/height for each subplot's map frame. |
130 | 128 |
|
131 | 129 | # %% |
132 | 130 | # .. tip:: |
133 | 131 | # |
134 | | -# In the above example, we used the following commands to activate the |
135 | | -# four subplots explicitly one after another:: |
| 132 | +# In the above example, we used the following commands to activate the four subplots |
| 133 | +# explicitly one after another:: |
136 | 134 | # |
137 | 135 | # fig.basemap(..., panel=[0, 0]) |
138 | 136 | # fig.basemap(..., panel=[0, 1]) |
139 | 137 | # fig.basemap(..., panel=[1, 0]) |
140 | 138 | # fig.basemap(..., panel=[1, 1]) |
141 | 139 | # |
142 | | -# In fact, we can just use ``fig.basemap(..., panel=True)`` without |
143 | | -# specifying any subplot index number, and GMT will automatically activate |
144 | | -# the next subplot panel. |
| 140 | +# In fact, we can just use ``fig.basemap(..., panel=True)`` without specifying any |
| 141 | +# subplot index number, and GMT will automatically activate the next subplot panel. |
145 | 142 |
|
146 | 143 | # %% |
147 | 144 | # .. note:: |
148 | 145 | # |
149 | | -# All plotting methods (e.g. :meth:`pygmt.Figure.coast`, |
150 | | -# :meth:`pygmt.Figure.text`, etc) are able to use ``panel`` parameter when |
151 | | -# in subplot mode. Once a panel is activated using ``panel`` or |
152 | | -# :meth:`pygmt.Figure.set_panel`, subsequent plotting commands that don't |
153 | | -# set a ``panel`` will have their elements added to the same panel as |
154 | | -# before. |
| 146 | +# All plotting methods (e.g. :meth:`pygmt.Figure.coast`, :meth:`pygmt.Figure.text`, |
| 147 | +# etc) are able to use ``panel`` parameter when in subplot mode. Once a panel is |
| 148 | +# activated using ``panel`` or :meth:`pygmt.Figure.set_panel`, subsequent plotting |
| 149 | +# commands that don't set a ``panel`` will have their elements added to the same |
| 150 | +# panel as before. |
155 | 151 |
|
156 | 152 |
|
157 | 153 | # %% |
158 | 154 | # Shared x- and y-axes |
159 | 155 | # -------------------- |
160 | 156 | # |
161 | | -# In the example above with the four subplots, the two subplots for each row |
162 | | -# have the same y-axis range, and the two subplots for each column have the |
163 | | -# same x-axis range. You can use the ``sharex``/``sharey`` parameters to set a |
164 | | -# common x- and/or y-axis between subplots. |
| 157 | +# In the example above with the four subplots, the two subplots for each row have the |
| 158 | +# same y-axis range, and the two subplots for each column have the same x-axis range. |
| 159 | +# You can use the ``sharex``/``sharey`` parameters to set a common x- and/or y-axis |
| 160 | +# between subplots. |
165 | 161 |
|
166 | 162 | fig = pygmt.Figure() |
167 | 163 | with fig.subplot( |
168 | 164 | nrows=2, |
169 | 165 | ncols=2, |
170 | 166 | figsize=("15c", "6c"), # width of 15 cm, height of 6 cm |
171 | | - autolabel=True, |
| 167 | + tag=True, |
172 | 168 | margins=["0.3c", "0.2c"], # horizontal 0.3 cm and vertical 0.2 cm margins |
173 | 169 | title="My Subplot Heading", |
174 | 170 | sharex="b", # shared x-axis on the bottom side |
|
182 | 178 | fig.show() |
183 | 179 |
|
184 | 180 | # %% |
185 | | -# ``sharex="b"`` indicates that subplots in a column will share the x-axis, and |
186 | | -# only the **b**\ ottom axis is displayed. ``sharey="l"`` indicates that |
187 | | -# subplots within a row will share the y-axis, and only the **l**\ eft axis is |
188 | | -# displayed. |
| 181 | +# ``sharex="b"`` indicates that subplots in a column will share the x-axis, and only the |
| 182 | +# **b**\ ottom axis is displayed. ``sharey="l"`` indicates that subplots within a row |
| 183 | +# will share the y-axis, and only the **l**\ eft axis is displayed. |
189 | 184 | # |
190 | | -# Of course, instead of using the ``sharex``/``sharey`` parameters, you can |
191 | | -# also set a different ``frame`` for each subplot to control the axis |
192 | | -# properties individually for each subplot. |
| 185 | +# Of course, instead of using the ``sharex``/``sharey`` parameters, you can also set a |
| 186 | +# different ``frame`` for each subplot to control the axis properties individually for |
| 187 | +# each subplot. |
193 | 188 |
|
194 | 189 |
|
195 | 190 | # %% |
196 | 191 | # Advanced subplot layouts |
197 | 192 | # ------------------------ |
198 | 193 | # |
199 | | -# Nested subplots are currently not supported. If you want to create more |
200 | | -# complex subplot layouts, some manual adjustments are needed. |
| 194 | +# Nested subplots are currently not supported. If you want to create more complex |
| 195 | +# subplot layouts, some manual adjustments are needed. |
201 | 196 | # |
202 | | -# The following example draws three subplots in a 2-row, 2-column layout, with |
203 | | -# the first subplot occupying the first row. |
| 197 | +# The following example draws three subplots in a 2-row, 2-column layout, with the first |
| 198 | +# subplot occupying the first row. |
204 | 199 |
|
205 | 200 | fig = pygmt.Figure() |
206 | 201 | # Bottom row, two subplots |
207 | | -with fig.subplot(nrows=1, ncols=2, figsize=("15c", "3c"), autolabel="b)"): |
| 202 | +with fig.subplot(nrows=1, ncols=2, figsize=("15c", "3c"), tag="b)"): |
208 | 203 | fig.basemap( |
209 | 204 | region=[0, 5, 0, 5], projection="X?", frame=["af", "WSne"], panel=[0, 0] |
210 | 205 | ) |
|
214 | 209 | # Move plot origin by 1 cm above the height of the entire figure |
215 | 210 | fig.shift_origin(yshift="h+1c") |
216 | 211 | # Top row, one subplot |
217 | | -with fig.subplot(nrows=1, ncols=1, figsize=("15c", "3c"), autolabel="a)"): |
| 212 | +with fig.subplot(nrows=1, ncols=1, figsize=("15c", "3c"), tag="a)"): |
218 | 213 | fig.basemap( |
219 | 214 | region=[0, 10, 0, 10], projection="X?", frame=["af", "WSne"], panel=[0, 0] |
220 | 215 | ) |
|
223 | 218 | fig.show() |
224 | 219 |
|
225 | 220 | # %% |
226 | | -# We start by drawing the bottom two subplots, setting ``autolabel="b)"`` so |
227 | | -# that the subplots are labelled 'b)' and 'c)'. Next, we use |
228 | | -# :meth:`pygmt.Figure.shift_origin` to move the plot origin 1 cm above the |
229 | | -# **h**\ eight of the entire figure that is currently plotted (i.e. the bottom |
230 | | -# row subplots). A single subplot is then plotted on the top row. You may need |
231 | | -# to adjust the ``yshift`` parameter to make your plot look nice. This top row |
232 | | -# uses ``autolabel="a)"``, and we also plotted some text inside. Note that |
233 | | -# ``projection="X?"`` was used to let GMT automatically determine the size of |
234 | | -# the subplot according to the size of the subplot area. |
| 221 | +# We start by drawing the bottom two subplots, setting ``tag="b)"`` so that the subplots |
| 222 | +# are tagged 'b)' and 'c)'. Next, we use :meth:`pygmt.Figure.shift_origin` to move the |
| 223 | +# plot origin 1 cm above the **h**\ eight of the entire figure that is currently plotted |
| 224 | +# (i.e. the bottom row subplots). A single subplot is then plotted on the top row. You |
| 225 | +# may need to adjust the ``yshift`` parameter to make your plot look nice. This top row |
| 226 | +# uses ``tag="a)"``, and we also plotted some text inside. Note that ``projection="X?"`` |
| 227 | +# was used to let GMT automatically determine the size of the subplot according to the |
| 228 | +# size of the subplot area. |
235 | 229 |
|
236 | 230 | # %% |
237 | | -# You can also manually override the ``autolabel`` for each subplot using for |
238 | | -# example, ``fig.set_panel(..., fixedlabel="b) Panel 2")`` which would allow |
239 | | -# you to manually label a single subplot as you wish. This can be useful for |
240 | | -# adding a more descriptive subtitle to individual subplots. |
| 231 | +# You can also manually override the ``tag`` for each subplot using for example, |
| 232 | +# ``fig.set_panel(..., fixedlabel="b) Panel 2")`` which would allow you to manually tag |
| 233 | +# a single subplot as you wish. This can be useful for adding a more descriptive |
| 234 | +# subtitle to individual subplots. |
241 | 235 |
|
242 | 236 | # sphinx_gallery_thumbnail_number = 3 |
0 commit comments