Skip to content

Commit 83f06c3

Browse files
Figure.subplot: Add parameters tag/tag_box/tag_position/tag_orientation/tag_number_style/tag_font for controlling subplot tagging (#4313)
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
1 parent e436168 commit 83f06c3

File tree

5 files changed

+353
-143
lines changed

5 files changed

+353
-143
lines changed

examples/gallery/embellishments/colorbars_multiple.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
Multiple colormaps
33
==================
44
5-
This gallery example shows how to create multiple colormaps for different
6-
subplots. To better understand how GMT modern mode maintains several levels of
7-
colormaps, please refer to
8-
:gmt-docs:`reference/features.html#gmt-modern-mode-hierarchical-levels` for
5+
This gallery example shows how to create multiple colormaps for different subplots. To
6+
better understand how GMT modern mode maintains several levels of colormaps, please
7+
refer to :gmt-docs:`reference/features.html#gmt-modern-mode-hierarchical-levels` for
98
details.
109
"""
1110

@@ -19,26 +18,24 @@
1918
subset_region = [-14, 30, 35, 60]
2019
grid_subset = pygmt.datasets.load_earth_relief(resolution="10m", region=subset_region)
2120

22-
# Define a 1-row, 2-column subplot layout. The overall figure dimensions are
23-
# set to be 15 cm wide and 8 cm high. Each subplot is automatically labelled.
24-
# The space between the subplots is set to be 0.5 cm.
25-
with fig.subplot(
26-
nrows=1, ncols=2, figsize=("15c", "8c"), autolabel=True, margins="0.5c"
27-
):
28-
# Activate the first panel so that the colormap created by the makecpt
29-
# function is a panel-level CPT
21+
# Define a 1-row, 2-column subplot layout. The overall figure dimensions are set to be
22+
# 15 cm wide and 8 cm high. Each subplot is automatically tagged. The space between the
23+
# subplots is set to be 0.5 cm.
24+
with fig.subplot(nrows=1, ncols=2, figsize=("15c", "8c"), tag=True, margins=0.5):
25+
# Activate the first panel so that the colormap created by the makecpt function is
26+
# a panel-level CPT
3027
with fig.set_panel(panel=0):
3128
pygmt.makecpt(cmap="gmt/geo", series=[-8000, 8000])
32-
# "R?" means Winkel Tripel projection with map width automatically
33-
# determined from the subplot width.
29+
# "R?" means Winkel Tripel projection with map width automatically determined
30+
# from the subplot width.
3431
fig.grdimage(grid=grid_globe, projection="R?", region="g", frame="a")
3532
fig.colorbar(frame=["a4000f2000", "x+lElevation", "y+lm"])
36-
# Activate the second panel so that the colormap created by the makecpt
37-
# function is a panel-level CPT
33+
# Activate the second panel so that the colormap created by the makecpt function is
34+
# a panel-level CPT
3835
with fig.set_panel(panel=1):
3936
pygmt.makecpt(cmap="gmt/globe", series=[-6000, 3000])
40-
# "M?" means Mercator projection with map width also automatically
41-
# determined from the subplot width.
37+
# "M?" means Mercator projection with map width also automatically determined
38+
# from the subplot width.
4239
fig.grdimage(grid=grid_subset, projection="M?", region=subset_region, frame="a")
4340
fig.colorbar(frame=["a2000f1000", "x+lElevation", "y+lm"])
4441

examples/tutorials/advanced/subplots.py

Lines changed: 80 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
Making subplots
33
===============
44
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.
88
99
There are two main ways to create subplots in GMT:
1010
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.
1313
- Use :meth:`pygmt.Figure.subplot` to define the layout of the subplots.
1414
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.
1919
"""
2020

2121
# %%
@@ -31,10 +31,10 @@
3131
# Define subplot layout
3232
# ---------------------
3333
#
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:
3838

3939
# %%
4040
# .. code-block:: default
@@ -44,10 +44,10 @@
4444

4545
# %%
4646
# 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:
5151

5252
with fig.subplot(nrows=2, ncols=3, figsize=("15c", "6c"), frame="lrtb"):
5353
for i in range(2): # row number starting from 0
@@ -62,45 +62,44 @@
6262
fig.show()
6363

6464
# %%
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``.
7170

7271
# %%
7372
# .. note::
7473
#
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.
7877

7978
# %%
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:
8382

8483
# %%
8584
# .. code-block:: default
8685
#
87-
# with fig.set_panel(panel=[0, 2]):
86+
# with fig.set_panel(panel=(0, 2)):
8887
# ...
8988

9089

9190
# %%
9291
# Making your first subplot
9392
# -------------------------
9493
#
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.
9796

9897
fig = pygmt.Figure()
9998
with fig.subplot(
10099
nrows=2,
101100
ncols=2,
102101
figsize=("15c", "6c"),
103-
autolabel=True,
102+
tag=True,
104103
frame=["af", "WSne"],
105104
margins=["0.1c", "0.2c"],
106105
title="My Subplot Heading",
@@ -113,62 +112,59 @@
113112

114113
# %%
115114
# 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:
119118
#
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.
124122
# - ``title="My Subplot Heading"``: Adds a title on top of the whole figure.
125123
#
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.
130128

131129
# %%
132130
# .. tip::
133131
#
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::
136134
#
137135
# fig.basemap(..., panel=[0, 0])
138136
# fig.basemap(..., panel=[0, 1])
139137
# fig.basemap(..., panel=[1, 0])
140138
# fig.basemap(..., panel=[1, 1])
141139
#
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.
145142

146143
# %%
147144
# .. note::
148145
#
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.
155151

156152

157153
# %%
158154
# Shared x- and y-axes
159155
# --------------------
160156
#
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.
165161

166162
fig = pygmt.Figure()
167163
with fig.subplot(
168164
nrows=2,
169165
ncols=2,
170166
figsize=("15c", "6c"), # width of 15 cm, height of 6 cm
171-
autolabel=True,
167+
tag=True,
172168
margins=["0.3c", "0.2c"], # horizontal 0.3 cm and vertical 0.2 cm margins
173169
title="My Subplot Heading",
174170
sharex="b", # shared x-axis on the bottom side
@@ -182,29 +178,28 @@
182178
fig.show()
183179

184180
# %%
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.
189184
#
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.
193188

194189

195190
# %%
196191
# Advanced subplot layouts
197192
# ------------------------
198193
#
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.
201196
#
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.
204199

205200
fig = pygmt.Figure()
206201
# 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)"):
208203
fig.basemap(
209204
region=[0, 5, 0, 5], projection="X?", frame=["af", "WSne"], panel=[0, 0]
210205
)
@@ -214,7 +209,7 @@
214209
# Move plot origin by 1 cm above the height of the entire figure
215210
fig.shift_origin(yshift="h+1c")
216211
# 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)"):
218213
fig.basemap(
219214
region=[0, 10, 0, 10], projection="X?", frame=["af", "WSne"], panel=[0, 0]
220215
)
@@ -223,20 +218,19 @@
223218
fig.show()
224219

225220
# %%
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.
235229

236230
# %%
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.
241235

242236
# sphinx_gallery_thumbnail_number = 3

0 commit comments

Comments
 (0)