Skip to content

Commit 8502e3b

Browse files
committed
Figure.subplot: Pythonic implemention for the subplot tagging
1 parent b243fef commit 8502e3b

1 file changed

Lines changed: 75 additions & 26 deletions

File tree

pygmt/src/subplot.py

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
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
@@ -23,10 +25,17 @@
2325
SR="sharey",
2426
)
2527
@kwargs_to_strings(Ff="sequence", Fs="sequence")
26-
def subplot(
28+
def subplot( # noqa: PLR0913
2729
self,
2830
nrows: int = 1,
2931
ncols: int = 1,
32+
autotag: str | bool = False,
33+
tag_box: Box | None = None,
34+
tag_offset: float | str | Sequence[float | str] | None = None,
35+
tag_position: AnchorCode | None = None,
36+
tag_orientation: Literal["horizontal", "vertical"] | None = None,
37+
tag_number_style: Literal["arabic", "roman", "Roman"] | None = None,
38+
tag_font: str | None = None,
3039
margins: float | str | Sequence[float | str] | None = None,
3140
title: str | None = None,
3241
projection: str | None = None,
@@ -67,31 +76,48 @@ def subplot(
6776
Specify the dimensions of each subplot directly as [*width*, *height*].
6877
Note that only one of ``figsize`` or ``subsize`` can be provided at
6978
once.
79+
autotag
80+
Specify automatic tagging of each subplot. It can accept a number, or a letter.
81+
The number or letter can be surrounded by parentheses on any side if these
82+
should be typeset as part of the tag. This sets the tag of the first, top-left
83+
subplot and others follow sequentially. If set to ``True``, default to ``"a)"``.
7084
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].
85+
Examples are:
86+
87+
- ``autotag="a"``: tags are ``a``, ``b``, ``c``, ...
88+
- ``autotag="1"``: tags are ``1``, ``2``, ``3``, ...
89+
- ``autotag="a)"``: tags are ``a)``, ``b)``, ``c)``, ...
90+
- ``autotag="(c)"``: tags are ``(c)``, ``(d)``, ``(e)``, ...
91+
- ``autotag=True``: same as ``autotag="a)"``.
92+
tag_box
93+
Draw a box around the subplot tag. See :class:`pygmt.params.Box` for details on
94+
how to specify the box.
95+
96+
**Notes on the use of the ``Box`` class:**
97+
98+
- The property ``clearance`` cannot be four values.
99+
- The property ``inner_pen``/``inner_gap``/``radius`` is not supported.
100+
tag_position
101+
Position of the subplot tag. It can be specified using a
102+
:doc:`2-character justification code </techref/justification_codes>`. If not
103+
specified, defaults to ``"TL"`` (Top Left corner).
104+
tag_offset
105+
Offset of the subplot tag in the direction implied by the justification. It can
106+
be a single value or a sequence of two values specifying the x- and y-offsets.
107+
[Default to ``"4p/4p"``, i.e., 20 % of the :gmt-term:`FONT_TAG` size].
108+
tag_orientation
109+
Orientation of the subplot tag. It can be:
110+
111+
- ``"horizontal"``: Increase tag numbers horizontally across rows [Default].
112+
- ``"vertical"``: Increase tag numbers vertically down columns.
113+
tag_number_style
114+
Style of the subplot tag numbers. It can be:
115+
116+
- ``"arabic"``: Arabic numerals: 1, 2, 3, ... [Default].
117+
- ``"roman"``: Lowercase Roman numerals: i, ii, iii, ...
118+
- ``"Roman"``: Uppercase Roman numerals: I, II, III, ...
119+
tag_font
120+
Font for the subplot tag. [Default to ``"20p,Helvetica,black"``].
95121
clearance : str or list
96122
[*side*]\ *clearance*.
97123
Reserve a space of dimension *clearance* between the margin and the
@@ -170,6 +196,22 @@ def subplot(
170196
raise GMTInvalidInput(msg)
171197

172198
aliasdict = AliasSystem(
199+
A=[
200+
Alias(autotag, name="autotag"),
201+
Alias(tag_box, name="tag_box"),
202+
Alias(tag_offset, name="tag_offset", prefix="+o", sep="/", size=2),
203+
Alias(tag_position, name="tag_position", prefix="+j"),
204+
Alias(
205+
tag_orientation,
206+
name="tag_orientation",
207+
mapping={"horizontal": "", "vertical": "+v"},
208+
),
209+
Alias(
210+
tag_number_style,
211+
name="tag_number_style",
212+
mapping={"arabic": "", "roman": "+r", "Roman": "+R"},
213+
),
214+
],
173215
M=Alias(margins, name="margins", sep="/", size=(2, 4)),
174216
T=Alias(title, name="title"),
175217
).add_common(
@@ -180,6 +222,9 @@ def subplot(
180222
)
181223
aliasdict.merge(kwargs)
182224

225+
# Configure FONT_TAG if tag_font is set
226+
confdict = {"FONT_TAG": tag_font} if tag_font is not None else {}
227+
183228
# Need to use separate sessions for "subplot begin" and "subplot end".
184229
# Otherwise, "subplot end" will use the last session, which may cause
185230
# strange positioning issues for later plotting calls.
@@ -188,7 +233,11 @@ def subplot(
188233
with Session() as lib:
189234
lib.call_module(
190235
module="subplot",
191-
args=["begin", f"{nrows}x{ncols}", *build_arg_list(aliasdict)],
236+
args=[
237+
"begin",
238+
f"{nrows}x{ncols}",
239+
*build_arg_list(aliasdict, confdict=confdict),
240+
],
192241
)
193242
yield
194243
finally:

0 commit comments

Comments
 (0)