Skip to content

Commit 4b6a925

Browse files
committed
Figure.subplot: Pythonic implemention for the subplot tagging
1 parent 886355f commit 4b6a925

File tree

1 file changed

+99
-27
lines changed

1 file changed

+99
-27
lines changed

pygmt/src/subplot.py

Lines changed: 99 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,35 @@
66
from collections.abc import Sequence
77
from typing import Literal
88

9+
from pygmt._typing import AnchorCode
910
from pygmt.alias import Alias, AliasSystem
1011
from pygmt.clib import Session
1112
from pygmt.exceptions import GMTInvalidInput, GMTValueError
1213
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
14+
from pygmt.params import Box
1315

1416

1517
@fmt_docstring
1618
@contextlib.contextmanager
1719
@use_alias(
1820
Ff="figsize",
1921
Fs="subsize",
20-
A="autolabel",
2122
C="clearance",
2223
SC="sharex",
2324
SR="sharey",
2425
)
2526
@kwargs_to_strings(Ff="sequence", Fs="sequence")
26-
def subplot(
27+
def subplot( # noqa: PLR0913
2728
self,
2829
nrows: int = 1,
2930
ncols: int = 1,
31+
autolabel: str | bool = False,
32+
autotag: str | bool = False,
33+
tag_box: Box | None = None,
34+
tag_position: AnchorCode | None = None,
35+
tag_orientation: Literal["horizontal", "vertical"] | None = None,
36+
tag_number_style: Literal["arabic", "roman", "Roman"] | None = None,
37+
tag_font: str | None = None,
3038
margins: float | str | Sequence[float | str] | None = None,
3139
title: str | None = None,
3240
projection: str | None = None,
@@ -67,31 +75,59 @@ def subplot(
6775
Specify the dimensions of each subplot directly as [*width*, *height*].
6876
Note that only one of ``figsize`` or ``subsize`` can be provided at
6977
once.
78+
autolabel
79+
Specify automatic tagging of each subplot.
7080
71-
autolabel : bool or str
72-
[*autolabel*][**+c**\ *dx*\ [/*dy*]][**+g**\ *fill*][**+j**\|\ **J**\
73-
*refpoint*][**+o**\ *dx*\ [/*dy*]][**+p**\ *pen*][**+r**\|\ **R**]\ [**+v**].
74-
Specify automatic tagging of each subplot. Append either a number or letter
75-
[Default is ``"a"``]. This sets the tag of the first, top-left subplot and
76-
others follow sequentially. Surround the number or letter by parentheses on
77-
any side if these should be typeset as part of the tag [Default is ``")"``].
78-
Use **+j**\|\ **J** for setting *refpoint* via a
79-
:doc:`2-character justification code </techref/justification_codes>`
80-
to specify where the tag should be placed in the subplot [Default is ``"TL"``
81-
for the Top Left corner]. **Note**: **+j** sets the justification of the tag
82-
to *refpoint* (suitable for interior tags) while **+J** instead selects the
83-
mirror opposite (suitable for exterior tags). Append **+c**\ *dx*\[/*dy*] to
84-
set the clearance between the tag and a surrounding text box requested via
85-
**+g** or **+p** [Default is ``"3p/3p"``, i.e., 15 % of the
86-
:gmt-term:`FONT_TAG` size dimension]. Append **+g**\ *fill* to paint the tag's
87-
text box with *fill* [Default is no fill]. Append **+o**\ *dx*\ [/*dy*] to
88-
offset the tag's reference point in the direction implied by the justification
89-
[Default is ``"4p/4p"``, i.e., 20 % of the :gmt-term:`FONT_TAG` size]. Append
90-
**+p**\ *pen* to draw the outline of the tag's text box using the selected *pen*
91-
[Default is no outline]. Append **+r** to typeset your tag numbers using
92-
lowercase Roman numerals; use **+R** for uppercase Roman numerals [Default is
93-
Arabic numerals]. Append **+v** to increase tag numbers vertically down columns
94-
[Default is horizontally across rows].
81+
.. deprecated:: v0.18.0
82+
83+
Use the parameters ``autotag``, ``tag_position``, ``tag_box``,
84+
``tag_number_style``, ``tag_orientation``, and ``tag_font`` instead.
85+
autotag
86+
Specify automatic tagging of each subplot. It can accept a number, or a letter.
87+
The number or letter can be surrounded by parentheses on any side if these
88+
should be typeset as part of the tag. This sets the tag of the first, top-left
89+
subplot and others follow sequentially. If set to ``True``, default to ``"a)"``.
90+
91+
Examples are:
92+
93+
- ``autotag="a"``: tags are ``a``, ``b``, ``c``, ...
94+
- ``autotag="1"``: tags are ``1``, ``2``, ``3``, ...
95+
- ``autotag="a)"``: tags are ``a)``, ``b)``, ``c)``, ...
96+
- ``autotag="(c)"``: tags are ``(c)``, ``(d)``, ``(e)``, ...
97+
- ``autotag=True``: same as ``autotag="a)"``.
98+
tag_position
99+
Position of the subplot tag on the plot. It can be specified in two ways:
100+
101+
- A :doc:`2-character justification code </techref/justification_codes>` for a
102+
position inside the plot, e.g., ``"TL"`` for Top Left corner inside the plot.
103+
- A :class:`pygmt.params.Position` object to fully control the position and
104+
offset. **Note**: the ``refpoint`` propterty of the Position object must be
105+
an two-character justification code, and ``cstype`` must be set to either
106+
``"inside"`` or ``"outside"``,
107+
108+
If not specified, defaults to Top Left corner inside the plot with the offset
109+
default to ``("4p", "4p")``, i.e., 20% of the :gmt-term:`FONT_TAG` size.
110+
tag_box
111+
Draw a box around the subplot tag. See :class:`pygmt.params.Box` for details on
112+
how to specify the box.
113+
114+
**Notes on the use of the ``Box`` class:**
115+
116+
- The property ``clearance`` only accept one or two values.
117+
- The property ``inner_pen``/``inner_gap``/``radius`` is not supported.
118+
tag_number_style
119+
Style of the subplot tag numbers. It can be:
120+
121+
- ``"arabic"``: Arabic numerals: 1, 2, 3, ... [Default].
122+
- ``"roman"``: Lowercase Roman numerals: i, ii, iii, ...
123+
- ``"Roman"``: Uppercase Roman numerals: I, II, III, ...
124+
tag_orientation
125+
Orientation of the subplot tag. It can be:
126+
127+
- ``"horizontal"``: Increase tag numbers horizontally across rows [Default].
128+
- ``"vertical"``: Increase tag numbers vertically down columns.
129+
tag_font
130+
Font for the subplot tag [Default to ``"20p,Helvetica,black"``].
95131
clearance : str or list
96132
[*side*]\ *clearance*.
97133
Reserve a space of dimension *clearance* between the margin and the
@@ -169,7 +205,36 @@ def subplot(
169205
msg = "Please provide either one of 'figsize' or 'subsize' only."
170206
raise GMTInvalidInput(msg)
171207

208+
# Check conflicts with deprecated 'autolabel' parameter.
209+
if autolabel:
210+
if any(
211+
v is not None and v is not False
212+
for v in [autotag, tag_position, tag_box, tag_number_style, tag_orientation]
213+
):
214+
msg = (
215+
"The 'autolabel' parameter is deprecated since v0.18.0. "
216+
"Please use the parameters 'autotag', 'tag_position', 'tag_box', "
217+
"'tag_number_style', 'tag_orientation', and 'tag_font' instead."
218+
)
219+
raise GMTInvalidInput(msg)
220+
kwargs["A"] = autolabel # Use 'A' internally for 'autotag'.
221+
172222
aliasdict = AliasSystem(
223+
A=[
224+
Alias(autotag, name="autotag"),
225+
Alias(tag_position, name="tag_position", prefix="+"), # Prefix is "+".
226+
Alias(tag_box, name="tag_box"),
227+
Alias(
228+
tag_number_style,
229+
name="tag_number_style",
230+
mapping={"arabic": "", "roman": "+r", "Roman": "+R"},
231+
),
232+
Alias(
233+
tag_orientation,
234+
name="tag_orientation",
235+
mapping={"horizontal": "", "vertical": "+v"},
236+
),
237+
],
173238
M=Alias(margins, name="margins", sep="/", size=(2, 4)),
174239
T=Alias(title, name="title"),
175240
).add_common(
@@ -180,6 +245,9 @@ def subplot(
180245
)
181246
aliasdict.merge(kwargs)
182247

248+
# Configure FONT_TAG if tag_font is set
249+
confdict = {"FONT_TAG": tag_font} if tag_font is not None else {}
250+
183251
# Need to use separate sessions for "subplot begin" and "subplot end".
184252
# Otherwise, "subplot end" will use the last session, which may cause
185253
# strange positioning issues for later plotting calls.
@@ -188,7 +256,11 @@ def subplot(
188256
with Session() as lib:
189257
lib.call_module(
190258
module="subplot",
191-
args=["begin", f"{nrows}x{ncols}", *build_arg_list(aliasdict)],
259+
args=[
260+
"begin",
261+
f"{nrows}x{ncols}",
262+
*build_arg_list(aliasdict, confdict=confdict),
263+
],
192264
)
193265
yield
194266
finally:

0 commit comments

Comments
 (0)