Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
DOC: Emphasize artist as annotation in AnnotationBbox demo and add to annotation guide#29374
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
story645 wants to merge1 commit intomatplotlib:mainChoose a base branch fromstory645:annotationbbox
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
137 changes: 87 additions & 50 deletionsgalleries/examples/text_labels_and_annotations/demo_annotation_box.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
""" | ||
====================== | ||
Artists as annotations | ||
====================== | ||
`.AnnotationBbox`facilitates annotating parts of the figure or axes using arbitrary | ||
artists, such as texts, images, and `matplotlib.patches`. `.AnnotationBbox` supports | ||
these artists via inputs that are subclassesof`.OffsetBox`, which is a class of | ||
container artists for positioning an artist relative to a parent artist. | ||
""" | ||
import matplotlib.pyplot as plt | ||
@@ -15,57 +15,60 @@ | ||
from matplotlib.cbook import get_sample_data | ||
from matplotlib.offsetbox import (AnnotationBbox, DrawingArea, OffsetImage, | ||
TextArea) | ||
from matplotlib.patches importAnnulus,Circle, ConnectionPatch | ||
# %%%% | ||
# Text | ||
# ==== | ||
# | ||
# `.AnnotationBbox` supports positioning annotations relative to data, Artists, and | ||
story645 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
# callables, as described in :ref:`annotations`. The `.TextArea` is used to create a | ||
# textbox that is not explicitly attached to an axes, which allows it to be used for | ||
# annotating figure objects. The `.annotate` method should be used when annotating an | ||
# axes element (such as a plot) with text. | ||
# | ||
fig, axd = plt.subplot_mosaic([['t1', '.', 't2']], layout='compressed') | ||
# Define a 1st position to annotate (display it with a marker) | ||
xy1 = (.25, .75) | ||
xy2 = (.75, .25) | ||
axd['t1'].plot(*xy1, ".r") | ||
axd['t2'].plot(*xy2, ".r") | ||
axd['t1'].set(xlim=(0, 1), ylim=(0, 1), aspect='equal') | ||
axd['t2'].set(xlim=(0, 1), ylim=(0, 1), aspect='equal') | ||
# Draw an arrow between the points | ||
c = ConnectionPatch(xyA=xy1, xyB=xy2, | ||
coordsA=axd['t1'].transData, coordsB=axd['t2'].transData, | ||
arrowstyle='->') | ||
fig.add_artist(c) | ||
# Annotate the ConnectionPatch position ('Test 1') | ||
offsetbox = TextArea("Test 1") | ||
ab1 = AnnotationBbox(offsetbox,(.5, .5), | ||
xybox=(0, 0), | ||
xycoords=c, | ||
boxcoords="offset points", | ||
arrowprops=dict(arrowstyle="->"), | ||
bboxprops=dict(boxstyle="sawtooth")) | ||
fig.add_artist(ab1) | ||
# %%%% | ||
# Images | ||
# ====== | ||
# The `.OffsetImage` container supports plotting images. `.OffsetImage` accepts many of | ||
# the same parameters as other image plotting methods, such as *cmap*, *norm*, and | ||
# *interpolation*. | ||
fig, ax = plt.subplots() | ||
# Define a position to annotate (don't display with a marker) | ||
xy = [0.3, 0.55] | ||
# Annotate a position with an image generated from an array of pixels | ||
arr = np.arange(100).reshape((10, 10)) | ||
im = OffsetImage(arr, zoom=2, cmap='viridis') | ||
im.image.axes = ax | ||
ab = AnnotationBbox(im, xy, | ||
@@ -74,10 +77,9 @@ | ||
boxcoords="offset points", | ||
pad=0.3, | ||
arrowprops=dict(arrowstyle="->")) | ||
ax.add_artist(ab) | ||
# Annotate the position with another image (a Grace Hopper portrait) | ||
with get_sample_data("grace_hopper.jpg") as file: | ||
arr_img = plt.imread(file) | ||
@@ -102,6 +104,38 @@ | ||
plt.show() | ||
# %%%% | ||
# Arbitrary Artists | ||
# ================ | ||
# | ||
# Multiple and arbitrary artists can be placed inside a `.DrawingArea`. | ||
fig, ax = plt.subplots() | ||
# Define a position to annotate (don't display with a marker) | ||
xy = [0.3, 0.55] | ||
# Annotate the position with a circle and annulus | ||
da = DrawingArea(30, 30, 0, 0) | ||
p = Circle((10, 10), 10, color='C0') | ||
da.add_artist(p) | ||
q = Annulus((20, 20), 10, 5, color='C1') | ||
da.add_artist(q) | ||
# Use the drawing area as an annotation | ||
ab = AnnotationBbox(da, xy, | ||
xybox=(.75, xy[1]), | ||
xycoords='data', | ||
boxcoords=("axes fraction", "data"), | ||
box_alignment=(0.2, 0.5), | ||
arrowprops=dict(arrowstyle="->"), | ||
bboxprops=dict(alpha=0.5)) | ||
ax.add_artist(ab) | ||
plt.show() | ||
# | ||
# %% | ||
# | ||
# .. admonition:: References | ||
@@ -117,3 +151,6 @@ | ||
# - `matplotlib.cbook.get_sample_data` | ||
# - `matplotlib.pyplot.subplots` | ||
# - `matplotlib.pyplot.imread` | ||
# | ||
# .. tags:: | ||
# component: annotation, styling: position |
45 changes: 44 additions & 1 deletiongalleries/users_explain/text/annotations.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.