Skip to content

Commit d5f6807

Browse files
committed
Clean up of demo_annotation_box.py
1 parent c718e61 commit d5f6807

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed
+21-15
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)