Skip to content

Commit 3ba53af

Browse files
authored
Figure.text: Support passing in a list of angle/font/justify values (#2720)
1 parent 9217078 commit 3ba53af

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

pygmt/src/text.py

+16-22
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
@kwargs_to_strings(
4040
R="sequence",
4141
textfiles="sequence_space",
42-
angle="sequence_comma",
43-
font="sequence_comma",
44-
justify="sequence_comma",
4542
c="sequence_comma",
4643
p="sequence",
4744
)
@@ -97,20 +94,20 @@ def text_(
9794
of the map.
9895
text : str or 1-D array
9996
The text string, or an array of strings to plot on the figure.
100-
angle: float, str, or bool
97+
angle: float, str, bool or list
10198
Set the angle measured in degrees counter-clockwise from
10299
horizontal (e.g. 30 sets the text at 30 degrees). If no angle is
103100
explicitly given (i.e. ``angle=True``) then the input to ``textfiles``
104101
must have this as a column.
105-
font : str or bool
102+
font : str, bool or list of str
106103
Set the font specification with format *size*\ ,\ *font*\ ,\ *color*
107104
where *size* is text size in points, *font* is the font to use, and
108105
*color* sets the font color. For example,
109106
``font="12p,Helvetica-Bold,red"`` selects a 12p, red, Helvetica-Bold
110107
font. If no font info is explicitly given (i.e. ``font=True``), then
111108
the input to ``textfiles`` must have this information in one of its
112109
columns.
113-
justify : str or bool
110+
justify : str, bool or list of str
114111
Set the alignment which refers to the part of the text string that
115112
will be mapped onto the (x, y) point. Choose a two-letter
116113
combination of **L**, **C**, **R** (for left, center, or right) and
@@ -170,7 +167,7 @@ def text_(
170167
``x``/``y`` and ``text``.
171168
{wrap}
172169
"""
173-
# pylint: disable=too-many-branches
170+
# pylint: disable=too-many-branches,too-many-locals
174171
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
175172

176173
# Ensure inputs are either textfiles, x/y/text, or position/text
@@ -201,25 +198,22 @@ def text_(
201198
):
202199
kwargs.update({"F": ""})
203200

204-
if angle is True:
205-
kwargs["F"] += "+a"
206-
elif isinstance(angle, (int, float, str)):
207-
kwargs["F"] += f"+a{str(angle)}"
208-
209-
if font is True:
210-
kwargs["F"] += "+f"
211-
elif isinstance(font, str):
212-
kwargs["F"] += f"+f{font}"
213-
214-
if justify is True:
215-
kwargs["F"] += "+j"
216-
elif isinstance(justify, str):
217-
kwargs["F"] += f"+j{justify}"
201+
extra_arrays = []
202+
for arg, flag in [(angle, "+a"), (font, "+f"), (justify, "+j")]:
203+
if arg is True:
204+
kwargs["F"] += flag
205+
elif is_nonstr_iter(arg):
206+
kwargs["F"] += flag
207+
if flag == "+a": # angle is numeric type
208+
extra_arrays.append(np.atleast_1d(arg))
209+
else: # font or justify is str type
210+
extra_arrays.append(np.atleast_1d(arg).astype(str))
211+
elif isinstance(arg, (int, float, str)):
212+
kwargs["F"] += f"{flag}{arg}"
218213

219214
if isinstance(position, str):
220215
kwargs["F"] += f"+c{position}+t{text}"
221216

222-
extra_arrays = []
223217
# If an array of transparency is given, GMT will read it from
224218
# the last numerical column per data record.
225219
if kwargs.get("t") is not None and is_nonstr_iter(kwargs["t"]):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
outs:
2+
- md5: c111539b0b5966eee4305f9485c85bd0
3+
size: 8208
4+
hash: md5
5+
path: test_text_angle_justify_font_arrays.png

pygmt/tests/test_text.py

+36
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,42 @@ def test_text_angle_font_justify_from_textfile():
311311
return fig
312312

313313

314+
@pytest.mark.mpl_image_compare(filename="test_text_position.png")
315+
def test_text_justify_array(region):
316+
"""
317+
Test passing an array of justify codes.
318+
319+
Re-use the baseline image from test_text_position().
320+
"""
321+
fig = Figure()
322+
fig.basemap(region=region, projection="x1c", frame="a")
323+
fig.text(
324+
x=[0, 2.5, 5.0, 0, 2.5, 5.0, 0, 2.5, 5.0],
325+
y=[0, 0, 0, 1.25, 1.25, 1.25, 2.5, 2.5, 2.5],
326+
justify=["BL", "BC", "BR", "ML", "MC", "MR", "TL", "TC", "TR"],
327+
text=["BL", "BC", "BR", "ML", "C M", "MR", "TL", "TC", "TR"],
328+
)
329+
return fig
330+
331+
332+
@pytest.mark.mpl_image_compare
333+
def test_text_angle_justify_font_arrays(region):
334+
"""
335+
Test passing arrays of angle, justify and font.
336+
"""
337+
fig = Figure()
338+
fig.basemap(region=region, projection="X5c/2.5c", frame=True)
339+
fig.text(
340+
x=[2.5, 2.5],
341+
y=[1.0, 2.0],
342+
angle=[30, 50],
343+
justify=["TL", "BR"],
344+
font=["15p,Helvetica-Bold,red", "5p,Times-Italic,blue"],
345+
text=["TEXT1", "TEXT2 with spaces"],
346+
)
347+
return fig
348+
349+
314350
@pytest.mark.mpl_image_compare
315351
def test_text_transparency():
316352
"""

0 commit comments

Comments
 (0)