From 0554d6f6412c1b30b591b26f38bdcb0d17eec6a3 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 9 Aug 2024 20:22:31 +0800 Subject: [PATCH 1/5] Add a tutorial for typeseting non-ASCII characters --- examples/tutorials/advanced/non_ascii_text.py | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/tutorials/advanced/non_ascii_text.py diff --git a/examples/tutorials/advanced/non_ascii_text.py b/examples/tutorials/advanced/non_ascii_text.py new file mode 100644 index 00000000000..15737f5009c --- /dev/null +++ b/examples/tutorials/advanced/non_ascii_text.py @@ -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 From f60da9d4fbccebab6d0c03ee3a86d050d8feb320 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 11 Aug 2024 17:55:33 +0800 Subject: [PATCH 2/5] Fix a typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- examples/tutorials/advanced/non_ascii_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tutorials/advanced/non_ascii_text.py b/examples/tutorials/advanced/non_ascii_text.py index 15737f5009c..a916c3c4a52 100644 --- a/examples/tutorials/advanced/non_ascii_text.py +++ b/examples/tutorials/advanced/non_ascii_text.py @@ -3,7 +3,7 @@ Typesetting non-ASCII text -------------------------- -In addtion to ASCII printable characters, sometimes you may also want to typeset +In addition 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. From 136a0edfff1acf9ea8c67414e278aae534c75f7d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 11 Aug 2024 19:44:16 +0800 Subject: [PATCH 3/5] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- examples/tutorials/advanced/non_ascii_text.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/tutorials/advanced/non_ascii_text.py b/examples/tutorials/advanced/non_ascii_text.py index a916c3c4a52..6cbf21a0021 100644 --- a/examples/tutorials/advanced/non_ascii_text.py +++ b/examples/tutorials/advanced/non_ascii_text.py @@ -56,7 +56,7 @@ # %% # Here are some important tips when using non-ASCII characters: # -# - **Similar-Looking Characters**: Be cautious when using characters that appear +# - **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 @@ -65,13 +65,12 @@ # 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 +# example, you cannot mix characters from "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 +# - **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 From 1d9fd0123be81473b06b2105f92cecbe6c964716 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 11 Aug 2024 19:59:19 +0800 Subject: [PATCH 4/5] Fix link to pygmt.Figure.text --- examples/tutorials/advanced/non_ascii_text.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/tutorials/advanced/non_ascii_text.py b/examples/tutorials/advanced/non_ascii_text.py index 6cbf21a0021..bae3c90426f 100644 --- a/examples/tutorials/advanced/non_ascii_text.py +++ b/examples/tutorials/advanced/non_ascii_text.py @@ -69,8 +69,8 @@ # 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. +# you have them in a text file and pass it to :meth:`pygmt.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 From aa61ae70a7be3ac0e17a20934c719f40cc15e538 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Tue, 13 Aug 2024 10:40:08 +0800 Subject: [PATCH 5/5] Update the order of text strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- examples/tutorials/advanced/non_ascii_text.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/examples/tutorials/advanced/non_ascii_text.py b/examples/tutorials/advanced/non_ascii_text.py index bae3c90426f..d51a8084281 100644 --- a/examples/tutorials/advanced/non_ascii_text.py +++ b/examples/tutorials/advanced/non_ascii_text.py @@ -33,24 +33,25 @@ 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:"], + y=[5, 4, 3, 2, 1], + text=["ASCII:", "ISOLatin1+:", "Symbol:", "ZapfDingbats:", "Mixed:"], font="20p,Helvetica-Bold,red", justify="LM", ) fig.text( x=[2, 2, 2, 2, 2], - y=[1, 2, 3, 4, 5], + y=[5, 4, 3, 2, 1], 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 + "±°ÀÁÂÃÄÅÈÌÒÙàèìòù", # Non-ASCII characters from Adobe ISOLatin1+ + "αβγδεζηθ⊗⊕∅⊃⊇⊄⊂⊆", # Non-ASCII characters from Adobe Symbol + "✈♥♦♣♠❛❜❝❞❨❩❪❫❬❭❮❯→↔", # Non-ASCII characters from Adobe ZapfDingbats + "ABCD αβγδ ①②③ ➊➋➌", # Mix characters from ISOLatin1+, Symbol and ZapfDingbats ], font="18p,Helvetica", justify="LM", ) + fig.show() # %%