Skip to content

Commit 3861421

Browse files
Add a tutorial for typesetting non-ASCII characters (#3389)
Co-authored-by: Yvonne Fröhlich <[email protected]>
1 parent dbb5c33 commit 3861421

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# ruff: noqa: RUF001,RUF003
2+
"""
3+
Typesetting non-ASCII text
4+
--------------------------
5+
6+
In addition to ASCII printable characters, sometimes you may also want to typeset
7+
non-ASCII characters on the plot, such as Greek letters, mathematical symbols, or
8+
special characters.
9+
10+
Due to the limitations of the underlying PostScript language, PyGMT doesn't support
11+
all characters in the Unicode standard. Instead, PyGMT supports a limited set of
12+
characters in the "Adobe Symbol", "Adobe ZapfDingbats", "Adobe ISOLatin1+", and
13+
"ISO-8859-*x*" (*x* can be 1-11, 13-16) encodings. Refer to :doc:`/techref/encodings`
14+
for the complete list of supported characters.
15+
16+
In PyGMT, the supported (ASCII and non-ASCII) characters can be directly used in the
17+
``text`` parameter of the :meth:`pygmt.Figure.text` method for typesetting text strings.
18+
They can also be used in the arguments of other plotting functions (e.g., in the
19+
``frame`` parameter to set the labels or title).
20+
21+
In this example, we demonstrate how to typeset non-ASCII characters in PyGMT.
22+
"""
23+
24+
# %%
25+
import pygmt
26+
27+
fig = pygmt.Figure()
28+
fig.basemap(
29+
region=[0, 5, 0, 6],
30+
projection="X14c/7c",
31+
frame=["xaf+lDistance (°)", "yaf+lValue (‰)", "WSen+tTitle: α² ± β²"],
32+
)
33+
34+
fig.text(
35+
x=[0.2, 0.2, 0.2, 0.2, 0.2],
36+
y=[5, 4, 3, 2, 1],
37+
text=["ASCII:", "ISOLatin1+:", "Symbol:", "ZapfDingbats:", "Mixed:"],
38+
font="20p,Helvetica-Bold,red",
39+
justify="LM",
40+
)
41+
fig.text(
42+
x=[2, 2, 2, 2, 2],
43+
y=[5, 4, 3, 2, 1],
44+
text=[
45+
"ABCDE12345!#$:;<=>?", # ASCII only
46+
"±°ÀÁÂÃÄÅÈÌÒÙàèìòù", # Non-ASCII characters from Adobe ISOLatin1+
47+
"αβγδεζηθ⊗⊕∅⊃⊇⊄⊂⊆", # Non-ASCII characters from Adobe Symbol
48+
"✈♥♦♣♠❛❜❝❞❨❩❪❫❬❭❮❯→↔", # Non-ASCII characters from Adobe ZapfDingbats
49+
"ABCD αβγδ ①②③ ➊➋➌", # Mix characters from ISOLatin1+, Symbol and ZapfDingbats
50+
],
51+
font="18p,Helvetica",
52+
justify="LM",
53+
)
54+
55+
fig.show()
56+
57+
# %%
58+
# Here are some important tips when using non-ASCII characters:
59+
#
60+
# - **Similar-looking characters**: Be cautious when using characters that appear
61+
# visually similar but are distinct. For example, ``Ω`` (OHM SIGN) and ``Ω`` (GREEK
62+
# CAPITAL LETTER OMEGA) may look alike, but PyGMT only supports the latter. Using the
63+
# incorrect character can lead to unexpected results. To avoid this, it's recommended
64+
# to copy and paste characters from the :doc:`/techref/encodings` documentation.
65+
# - **Mix characters from different encodings**: As shown in the example above, you can
66+
# mix characters from different encodings in the same text string. However, due to the
67+
# limitations of the underlying PostScript language, you cannot mix characters from
68+
# the "Adobe ISOLatin1+" and "ISO-8859-*x*" encodings in the same text string. For
69+
# example, you cannot mix characters from "Adobe ISOLatin1+" and "ISO-8859-2". If
70+
# you need to use characters from different encodings, you can use them in different
71+
# PyGMT function/method calls.
72+
# - **Non-ASCII characters in text files**: Non-ASCII characters are not supported if
73+
# you have them in a text file and pass it to :meth:`pygmt.Figure.text`. In this case,
74+
# you may want to load the text file into :class:`pandas.DataFrame` and then pass it
75+
# to the ``text`` parameter.
76+
77+
# sphinx_gallery_thumbnail_number = 1

0 commit comments

Comments
 (0)