Skip to content

ENH: fig.annotate #28753

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 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions galleries/examples/text_labels_and_annotations/rainbow_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import matplotlib.pyplot as plt

plt.rcParams["font.size"] = 20

ax = plt.figure().add_subplot(xticks=[], yticks=[])

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

plt.show()

# %%
#
# Figure Text
# -----------
#
# Figure text can be concatenated in a similar manner by creating a `~.text.Annotation`
# object and adding it to the figure:


import matplotlib.text as mtext

Check failure on line 43 in galleries/examples/text_labels_and_annotations/rainbow_text.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] reported by reviewdog 🐶 E402 module level import not at top of file Raw Output: ./galleries/examples/text_labels_and_annotations/rainbow_text.py:43:1: E402 module level import not at top of file

fig, ax = plt.subplots(subplot_kw=dict(visible=False))

text = fig.text(.1, .5, "Matplotlib", color="red")

text = mtext.Annotation(" says,", xycoords=text, xy=(1, 0),
va="bottom", color="gold", weight="bold")
fig.add_artist(text) # manually add artist to figure

text = mtext.Annotation(" hello", xycoords=text, xy=(1, 0),
va="bottom", color="green", style="italic")
fig.add_artist(text)

text = mtext.Annotation(" world!", xycoords=text, xy=(1, 0),
va="bottom", color="blue", family="serif")
fig.add_artist(text)

# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.axes.Axes.annotate`
# - `matplotlib.text.Annotation`
#
# .. tags:: component: annotation, component: text, styling: color
30 changes: 30 additions & 0 deletions galleries/users_explain/text/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,37 @@ def __call__(self, x0, y0, width, height, mutation_size):
connectionstyle="arc3,rad=-0.2",
relpos=(1., 0.),
fc="w"))
# %%
# Create Annotation Artist
# ^^^^^^^^^^^^^^^^^^^^^^^^
#
# An annotation artist may also be created using `.text.Annotation` and then added to
# the figure. This can be used to annotate other artists that are added to directly
# to the figure. For example:

import matplotlib.patches as mpatches
import matplotlib.text as mtext

fig, ax = plt.subplots(subplot_kw=dict(visible=False), figsize=(3, 3))

# add a circle and a square
circle = mpatches.Circle((.25, .75), radius=.15, fc='gold', ec='navy')
fig.add_artist(circle)
square = mpatches.Rectangle((.75, .25), height=.25, width=.25, fc='navy', ec='gold')
fig.add_artist(square)

# position annotation line between both artists
con = mtext.Annotation("", xy=(.95, .25), xytext=(0, 1),
xycoords=circle, textcoords=square,
arrowprops=dict(arrowstyle="-", lw=3, ls='--', color='navy'))
fig.add_artist(con)

# Position text relative to annotation
label = mtext.Annotation("connection", xy=(.5, .5), xytext=(1, 1),
xycoords=con, textcoords=fig.transFigure,
arrowprops=dict(arrowstyle="->", lw=3, ls=':', color='gold'),
size=20, color='navy', va='top', ha='right')
fig.add_artist(label)
# %%
# Placing Artist at anchored Axes locations
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ convention = "numpy"
"galleries/examples/style_sheets/bmh.py" = ["E501"]
"galleries/examples/subplots_axes_and_figures/demo_constrained_layout.py" = ["E402"]
"galleries/examples/text_labels_and_annotations/custom_legends.py" = ["E402"]
"galleries/examples/text_labels_and_annotations/rainbow_text.py" = ["E402"]
"galleries/examples/ticks/date_concise_formatter.py" = ["E402"]
"galleries/examples/ticks/date_formatters_locators.py" = ["F401"]
"galleries/examples/user_interfaces/embedding_in_gtk3_panzoom_sgskip.py" = ["E402"]
Expand Down
Loading