Skip to content

Commit c6eedb1

Browse files
story645tacaswell
andcommitted
document how to use text.Annotation for figure annotations and rainbow texts
Co-authored-by: Thomas A Caswell <tcaswell@gmail.com>
1 parent b01462c commit c6eedb1

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

galleries/examples/text_labels_and_annotations/rainbow_text.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import matplotlib.pyplot as plt
1414

1515
plt.rcParams["font.size"] = 20
16+
1617
ax = plt.figure().add_subplot(xticks=[], yticks=[])
1718

1819
# The first word, created with text().
@@ -29,3 +30,42 @@
2930
color="blue", family="serif") # custom properties
3031

3132
plt.show()
33+
34+
# %%
35+
#
36+
# Figure Text
37+
# -----------
38+
#
39+
# Figure text can be concatenated in a similar manner by creating a `~.text.Annotation`
40+
# object and adding it to the figure:
41+
42+
43+
import matplotlib.text as mtext
44+
45+
fig, ax = plt.subplots(subplot_kw=dict(visible=False))
46+
47+
text = fig.text(.1, .5, "Matplotlib", color="red")
48+
49+
text = mtext.Annotation(" says,", xycoords=text, xy=(1, 0),
50+
va="bottom", color="gold", weight="bold")
51+
fig.add_artist(text) # manually add artist to figure
52+
53+
text = mtext.Annotation(" hello", xycoords=text, xy=(1, 0),
54+
va="bottom", color="green", style="italic")
55+
fig.add_artist(text)
56+
57+
text = mtext.Annotation(" world!", xycoords=text, xy=(1, 0),
58+
va="bottom", color="blue", family="serif")
59+
fig.add_artist(text)
60+
61+
# %%
62+
#
63+
# .. admonition:: References
64+
#
65+
# The use of the following functions, methods, classes and modules is shown
66+
# in this example:
67+
#
68+
# - `matplotlib.axes.Axes.annotate`
69+
# - `matplotlib.text.Annotation`
70+
#
71+
# .. tags:: component: annotation, component: text, styling: color

galleries/users_explain/text/annotations.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,37 @@ def __call__(self, x0, y0, width, height, mutation_size):
586586
connectionstyle="arc3,rad=-0.2",
587587
relpos=(1., 0.),
588588
fc="w"))
589+
# %%
590+
# Create Annotation Artist
591+
# ^^^^^^^^^^^^^^^^^^^^^^^^
592+
#
593+
# An annotation artist may also be created using `.text.Annotation` and then added to
594+
# the figure. This can be used to annotate other artists that are added to directly
595+
# to the figure. For example:
596+
597+
import matplotlib.patches as mpatches
598+
import matplotlib.text as mtext
599+
600+
fig, ax = plt.subplots(subplot_kw=dict(visible=False), figsize=(3, 3))
601+
602+
# add a circle and a square
603+
circle = mpatches.Circle((.25, .75), radius=.15, fc='gold', ec='navy')
604+
fig.add_artist(circle)
605+
square = mpatches.Rectangle((.75, .25), height=.25, width=.25, fc='navy', ec='gold')
606+
fig.add_artist(square)
607+
608+
# position annotation line between both artists
609+
con = mtext.Annotation("", xy=(.95, .25), xytext=(0, 1),
610+
xycoords=circle, textcoords=square,
611+
arrowprops=dict(arrowstyle="-", lw=3, ls='--', color='navy'))
612+
fig.add_artist(con)
589613

614+
# Position text relative to annotation
615+
label = mtext.Annotation("connection", xy=(.5, .5), xytext=(1, 1),
616+
xycoords=con, textcoords=fig.transFigure,
617+
arrowprops=dict(arrowstyle="->", lw=3, ls=':', color='gold'),
618+
size=20, color='navy', va='top', ha='right')
619+
fig.add_artist(label)
590620
# %%
591621
# Placing Artist at anchored Axes locations
592622
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ convention = "numpy"
179179
"galleries/examples/style_sheets/bmh.py" = ["E501"]
180180
"galleries/examples/subplots_axes_and_figures/demo_constrained_layout.py" = ["E402"]
181181
"galleries/examples/text_labels_and_annotations/custom_legends.py" = ["E402"]
182+
"galleries/examples/text_labels_and_annotations/rainbow_text.py" = ["E402"]
182183
"galleries/examples/ticks/date_concise_formatter.py" = ["E402"]
183184
"galleries/examples/ticks/date_formatters_locators.py" = ["F401"]
184185
"galleries/examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py" = ["E402"]

0 commit comments

Comments
 (0)