Skip to content

Commit

Permalink
Add a tutorial for typeseting non-ASCII characters
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Aug 11, 2024
1 parent 60aef00 commit 0554d6f
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions examples/tutorials/advanced/non_ascii_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# ruff: noqa: RUF001,RUF003
"""
Typesetting non-ASCII text
--------------------------
In addtion to ASCII printable characters, sometimes you may also want to typeset
non-ASCII characters on the plot, such as Greek letters, mathematical symbols, or
special characters.
Due to the limitations of the underlying PostScript language, PyGMT doesn't support
all characters in the Unicode standard. Instead, PyGMT supports a limited set of
characters in the "Adobe Symbol", "Adobe ZapfDingbats", "Adobe ISOLatin1+", and
"ISO-8859-*x*" (*x* can be 1-11, 13-16) encodings. Refer to :doc:`/techref/encodings`
for the complete list of supported characters.
In PyGMT, the supported (ASCII and non-ASCII) characters can be directly used in the
``text`` parameter of the :meth:`pygmt.Figure.text` method for typesetting text strings.
They can also be used in the arguments of other plotting functions (e.g., in the
``frame`` parameter to set the labels or title).
In this example, we demonstrate how to typeset non-ASCII characters in PyGMT.
"""

# %%
import pygmt

fig = pygmt.Figure()
fig.basemap(
region=[0, 5, 0, 6],
projection="X14c/7c",
frame=["xaf+lDistance (°)", "yaf+lValue (‰)", "WSen+tTitle: α² ± β²"],
)

fig.text(
x=[0.2, 0.2, 0.2, 0.2, 0.2],
y=[1, 2, 3, 4, 5],
text=["Mixed:", "ZapfDingbats:", "Symbol:", "ISOLatin1+:", "ASCII:"],
font="20p,Helvetica-Bold,red",
justify="LM",
)
fig.text(
x=[2, 2, 2, 2, 2],
y=[1, 2, 3, 4, 5],
text=[
"ABCD αβγδ ①②③ ➊➋➌", # Mix characters from ISOLatin1+, Symbol and ZapfDingbats
"✈♥♦♣♠❛❜❝❞❨❩❪❫❬❭❮❯→↔", # Non-ASCII characters from Adobe ZapfDingbats
"αβγδεζηθ⊗⊕∅⊃⊇⊄⊂⊆", # Non-ASCII characters from Adobe Symbol
"±°ÀÁÂÃÄÅÈÌÒÙàèìòù", # Non-ASCII characters from Adobe ISOLatin1+
"ABCDE12345!#$:;<=>?", # ASCII only
],
font="18p,Helvetica",
justify="LM",
)
fig.show()

# %%
# Here are some important tips when using non-ASCII characters:
#
# - **Similar-Looking Characters**: Be cautious when using characters that appear
# visually similar but are distinct. For example, ``Ω`` (OHM SIGN) and ``Ω`` (GREEK
# CAPITAL LETTER OMEGA) may look alike, but PyGMT only supports the latter. Using the
# incorrect character can lead to unexpected results. To avoid this, it's recommended
# to copy and paste characters from the :doc:`/techref/encodings` documentation.
# - **Mix characters from different encodings**: As shown in the example above, you can
# mix characters from different encodings in the same text string. However, due to the
# limitations of the underlying PostScript language, you cannot mix characters from
# the "Adobe ISOLatin1+" and "ISO-8859-*x*" encodings in the same text string. For
# example, you cannot mix characters from the "Adobe ISOLatin1+" and "ISO-8859-2". If
# you need to use characters from different encodings, you can use them in different
# PyGMT function/method calls.
# - **Non-ASCII Characters in Text Files**: Non-ASCII characters are not supported if
# you have them in a text file and pass it to ``Figure.text``. In this case, you may
# want to load the text file into :class:`pandas.DataFrame` and then pass it to the
# ``text`` parameter.
#

# sphinx_gallery_thumbnail_number = 1

0 comments on commit 0554d6f

Please sign in to comment.