Skip to content

Commit 5ad2935

Browse files
committed
Update text
1 parent e01962f commit 5ad2935

File tree

1 file changed

+45
-54
lines changed

1 file changed

+45
-54
lines changed

pygmt/src/text.py

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
text - Plot text on a figure.
33
"""
44
import numpy as np
5+
from pygmt.alias import Alias, convert_aliases
56
from pygmt.clib import Session
67
from pygmt.exceptions import GMTInvalidInput
78
from pygmt.helpers import (
@@ -11,38 +12,12 @@
1112
is_nonstr_iter,
1213
kwargs_to_strings,
1314
non_ascii_to_octal,
14-
use_alias,
1515
)
1616

1717

1818
@fmt_docstring
19-
@use_alias(
20-
R="region",
21-
J="projection",
22-
B="frame",
23-
C="clearance",
24-
D="offset",
25-
G="fill",
26-
N="no_clip",
27-
V="verbose",
28-
W="pen",
29-
a="aspatial",
30-
c="panel",
31-
e="find",
32-
f="coltypes",
33-
h="header",
34-
it="use_word",
35-
p="perspective",
36-
t="transparency",
37-
w="wrap",
38-
)
39-
@kwargs_to_strings(
40-
R="sequence",
41-
textfiles="sequence_space",
42-
c="sequence_comma",
43-
p="sequence",
44-
)
45-
def text_( # noqa: PLR0912
19+
@kwargs_to_strings(textfiles="sequence_space")
20+
def text_( # noqa: PLR0913
4621
self,
4722
textfiles=None,
4823
x=None,
@@ -52,6 +27,9 @@ def text_( # noqa: PLR0912
5227
angle=None,
5328
font=None,
5429
justify=None,
30+
projection=None, # noqa: ARG001
31+
region=None, # noqa: ARG001
32+
transparency=None,
5533
**kwargs,
5634
):
5735
r"""
@@ -71,8 +49,6 @@ def text_( # noqa: PLR0912
7149
7250
Full option list at :gmt-docs:`text.html`
7351
74-
{aliases}
75-
7652
Parameters
7753
----------
7854
textfiles : str or list
@@ -182,6 +158,31 @@ def text_( # noqa: PLR0912
182158
"""
183159
kwargs = self._preprocess(**kwargs)
184160

161+
_aliases = [
162+
Alias("position", "F", "+c", ""),
163+
Alias("angle", "F", "+a", ""),
164+
Alias("font", "F", "+f", ""),
165+
Alias("justify", "F", "+j", ""),
166+
Alias("region", "R", "", "/"),
167+
Alias("projection", "J", "", ""),
168+
Alias("transparency", "t", "", ""),
169+
Alias("frame", "B", "", ""),
170+
Alias("clearance", "C", "", ""),
171+
Alias("offset", "D", "", ""),
172+
Alias("fill", "G", "", ""),
173+
Alias("no_clip", "N", "", ""),
174+
Alias("verbose", "V", "", ""),
175+
Alias("pen", "W", "", ""),
176+
Alias("aspatial", "a", "", ""),
177+
Alias("panel", "c", "", ","),
178+
Alias("find", "e", "", ""),
179+
Alias("coltypes", "f", "", ""),
180+
Alias("header", "h", "", ""),
181+
Alias("use_word", "it", "", ""),
182+
Alias("perspective", "p", "", "/"),
183+
Alias("wrap", "w", "", ""),
184+
]
185+
185186
# Ensure inputs are either textfiles, x/y/text, or position/text
186187
if position is None:
187188
if (x is not None or y is not None) and textfiles is not None:
@@ -201,33 +202,22 @@ def text_( # noqa: PLR0912
201202
kind = None
202203
textfiles = ""
203204

204-
# Build the -F option in gmt text.
205-
if kwargs.get("F") is None and any(
206-
v is not None for v in (position, angle, font, justify)
207-
):
208-
kwargs.update({"F": ""})
205+
# special handling with the position parameter
206+
if position is not None:
207+
position += f"+t{text}"
209208

210209
extra_arrays = []
211-
for arg, flag in [(angle, "+a"), (font, "+f"), (justify, "+j")]:
212-
if arg is True:
213-
kwargs["F"] += flag
214-
elif is_nonstr_iter(arg):
215-
kwargs["F"] += flag
216-
if flag == "+a": # angle is numeric type
217-
extra_arrays.append(np.atleast_1d(arg))
218-
else: # font or justify is str type
219-
extra_arrays.append(np.atleast_1d(arg).astype(str))
220-
elif isinstance(arg, (int, float, str)):
221-
kwargs["F"] += f"{flag}{arg}"
222-
223-
if isinstance(position, str):
224-
kwargs["F"] += f"+c{position}+t{text}"
225-
210+
# angle is numeric type
211+
if is_nonstr_iter(angle):
212+
extra_arrays.append(np.atleast_1d(angle))
213+
# font or justify is str type
214+
for arg in (font, justify):
215+
if is_nonstr_iter(arg):
216+
extra_arrays.append(np.atleast_1d(arg).astype(str)) # noqa: PERF401
226217
# If an array of transparency is given, GMT will read it from
227218
# the last numerical column per data record.
228-
if is_nonstr_iter(kwargs.get("t")):
229-
extra_arrays.append(kwargs["t"])
230-
kwargs["t"] = ""
219+
if is_nonstr_iter(transparency):
220+
extra_arrays.append(transparency)
231221

232222
# Append text at last column. Text must be passed in as str type.
233223
if kind == "vectors":
@@ -239,5 +229,6 @@ def text_( # noqa: PLR0912
239229
file_context = lib.virtualfile_from_data(
240230
check_kind="vector", data=textfiles, x=x, y=y, extra_arrays=extra_arrays
241231
)
232+
options = convert_aliases()
242233
with file_context as fname:
243-
lib.call_module(module="text", args=build_arg_string(kwargs, infile=fname))
234+
lib.call_module(module="text", args=build_arg_string(options, infile=fname))

0 commit comments

Comments
 (0)