Skip to content

Commit 604d077

Browse files
Figure.text: Support non-ASCII characters in the 'text' parameter (#2638)
Co-authored-by: Yvonne Fröhlich <[email protected]>
1 parent f620903 commit 604d077

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

pygmt/helpers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
data_kind,
2020
is_nonstr_iter,
2121
launch_external_viewer,
22+
non_ascii_to_octal,
2223
)

pygmt/src/text.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
fmt_docstring,
1111
is_nonstr_iter,
1212
kwargs_to_strings,
13+
non_ascii_to_octal,
1314
use_alias,
1415
)
1516

@@ -65,6 +66,12 @@ def text_(
6566
- ``x``/``y``, and ``text``
6667
- ``position`` and ``text``
6768
69+
The text strings passed via the ``text`` parameter can contain ASCII
70+
characters and non-ASCII characters defined in the ISOLatin1+ encoding
71+
(i.e., IEC_8859-1), and the Symbol and ZapfDingbats character sets.
72+
See :gmt-docs:`cookbook/octal-codes.html` for the full list of supported
73+
non-ASCII characters.
74+
6875
Full option list at :gmt-docs:`text.html`
6976
7077
{aliases}
@@ -223,7 +230,9 @@ def text_(
223230

224231
# Append text at last column. Text must be passed in as str type.
225232
if kind == "vectors":
226-
extra_arrays.append(np.atleast_1d(text).astype(str))
233+
extra_arrays.append(
234+
np.vectorize(non_ascii_to_octal)(np.atleast_1d(text).astype(str))
235+
)
227236

228237
with Session() as lib:
229238
file_context = lib.virtualfile_from_data(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
outs:
2+
- md5: aca7c6732bc8410a6299582a2bf3b997
3+
size: 17350
4+
hash: md5
5+
path: test_text_nonascii.png

pygmt/tests/test_text.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,16 @@ def test_text_nonstr_text():
371371
text=[1, 2, 3.0, 4.0],
372372
)
373373
return fig
374+
375+
376+
@pytest.mark.mpl_image_compare
377+
def test_text_nonascii():
378+
"""
379+
Test passing text strings with non-ascii characters.
380+
"""
381+
fig = Figure()
382+
fig.basemap(region=[0, 10, 0, 10], projection="X10c", frame=True)
383+
fig.text(position="TL", text="position-text:°α")
384+
fig.text(x=1, y=1, text="xytext:°α")
385+
fig.text(x=[5, 5], y=[3, 5], text=["xytext1:αζΔ❡", "xytext2:∑π∇✉"])
386+
return fig

0 commit comments

Comments
 (0)