Skip to content

DOC: Add gallery example to show text formatting #3987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
41 changes: 41 additions & 0 deletions examples/gallery/embellishments/text_formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Text formatting
===============

There are various options to format text added to a plot as well as the title of the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rephrase it to something like:

There are various options to format text strings added to a plot, e.g.,
text added via Figure.text, title of a plot, labels of colorbars,
Cartesian axes, and legend entries,

plot and labels of colorbars, Cartesian axes, and legend entries, including,
superscripts, subscripts, underlining and small caps (for an overview see
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
superscripts, subscripts, underlining and small caps (for an overview see
superscripts, subscripts, underlining, and small caps (for an overview, see

(:doc:`Text Formatting </techref/text_formatting>`). It's also possible to change the
font as well as its color and size only for specific characters of a longer text. The
supported fonts are listed at :doc:`Supported Fonts </techref/fonts>`.
"""

# %%
import pygmt

fig = pygmt.Figure()
fig.basemap(region=[-1, 1, -4, 4], projection="X4c/5c", frame=0)

# Change font color for specific characters of the word "PyGMT"
# blue for "P", yellow for "y", and red for "GMT"
fig.text(x=0, y=3, text="@;63/124/173;P@;;@;255/212/59;y@;;@;238/86/52;GMT@;;")

# Change font size and style for a single character, respectively
fig.text(x=0, y=2, text="te@:15:x@::t tex@%Courier-Oblique%t@%%")

# Superscript
fig.text(x=0, y=1, text="E = mc@+2@+")

# Subscripts and Greek letters
fig.text(x=0, y=0, text="@~s@~@-ij@- = c@-ijkl@- @~e@~@-kl@-")

# Combine two characters above each other
fig.text(x=0, y=-1, text="@!_~")

# Underline the text
fig.text(x=0, y=-2, text="@_underlined text@_")

# Use small caps
fig.text(x=0, y=-3, text="@#text in small caps@#")

fig.show()