Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2f2b08d

Browse filesBrowse files
authored
Merge pull request #6760 from afvincent/fix_demo_annotation_box
DOC: clean up of demo_annotation_box.py
2 parents b9aedb8 + d5f6807 commit 2f2b08d
Copy full SHA for 2f2b08d

File tree

Expand file treeCollapse file tree

1 file changed

+21
-15
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+21
-15
lines changed
+21-15Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import matplotlib.pyplot as plt
2-
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, \
3-
AnnotationBbox
2+
import numpy as np
3+
4+
from matplotlib.patches import Circle
5+
from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage,
6+
AnnotationBbox)
47
from matplotlib.cbook import get_sample_data
58

6-
import numpy as np
79

810
if 1:
911
fig, ax = plt.subplots()
1012

11-
offsetbox = TextArea("Test 1", minimumdescent=False)
12-
13+
# Define a 1st position to annotate (display it with a marker)
1314
xy = (0.5, 0.7)
14-
1515
ax.plot(xy[0], xy[1], ".r")
1616

17+
# Annotate the 1st position with a text box ('Test 1')
18+
offsetbox = TextArea("Test 1", minimumdescent=False)
19+
1720
ab = AnnotationBbox(offsetbox, xy,
1821
xybox=(-20, 40),
1922
xycoords='data',
2023
boxcoords="offset points",
2124
arrowprops=dict(arrowstyle="->"))
2225
ax.add_artist(ab)
2326

27+
# Annotate the 1st position with another text box ('Test')
2428
offsetbox = TextArea("Test", minimumdescent=False)
2529

2630
ab = AnnotationBbox(offsetbox, xy,
@@ -31,12 +35,14 @@
3135
arrowprops=dict(arrowstyle="->"))
3236
ax.add_artist(ab)
3337

34-
from matplotlib.patches import Circle
38+
# Define a 2nd position to annotate (don't display with a marker this time)
39+
xy = [0.3, 0.55]
40+
41+
# Annotate the 2nd position with a circle patch
3542
da = DrawingArea(20, 20, 0, 0)
3643
p = Circle((10, 10), 10)
3744
da.add_artist(p)
3845

39-
xy = [0.3, 0.55]
4046
ab = AnnotationBbox(da, xy,
4147
xybox=(1.02, xy[1]),
4248
xycoords='data',
@@ -46,6 +52,7 @@
4652

4753
ax.add_artist(ab)
4854

55+
# Annotate the 2nd position with an image (a generated array of pixels)
4956
arr = np.arange(100).reshape((10, 10))
5057
im = OffsetImage(arr, zoom=2)
5158
im.image.axes = ax
@@ -59,11 +66,9 @@
5966

6067
ax.add_artist(ab)
6168

62-
# another image
63-
64-
from matplotlib._png import read_png
69+
# Annotate the 2nd position with another image (a Grace Hopper portrait)
6570
fn = get_sample_data("grace_hopper.png", asfileobj=False)
66-
arr_img = read_png(fn)
71+
arr_img = plt.imread(fn, format='png')
6772

6873
imagebox = OffsetImage(arr_img, zoom=0.2)
6974
imagebox.image.axes = ax
@@ -73,14 +78,15 @@
7378
xycoords='data',
7479
boxcoords="offset points",
7580
pad=0.5,
76-
arrowprops=dict(arrowstyle="->",
77-
connectionstyle="angle,angleA=0,angleB=90,rad=3")
81+
arrowprops=dict(
82+
arrowstyle="->",
83+
connectionstyle="angle,angleA=0,angleB=90,rad=3")
7884
)
7985

8086
ax.add_artist(ab)
8187

88+
# Fix the display limits to see everything
8289
ax.set_xlim(0, 1)
8390
ax.set_ylim(0, 1)
8491

85-
plt.draw()
8692
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.