Skip to content

Commit 41bd991

Browse files
committed
Merge pull request matplotlib#8324 from DaveL17/patch-1
DOC: Update svg_tooltip.py
1 parent e035a3a commit 41bd991

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

examples/user_interfaces/svg_tooltip.py

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,22 @@
3131
fig, ax = plt.subplots()
3232

3333
# Create patches to which tooltips will be assigned.
34-
circle = plt.Circle((0, 0), 5, fc='blue')
35-
rect = plt.Rectangle((-5, 10), 10, 5, fc='green')
36-
37-
ax.add_patch(circle)
38-
ax.add_patch(rect)
39-
40-
# Create the tooltips
41-
circle_tip = ax.annotate(
42-
'This is a blue circle.',
43-
xy=(0, 0),
44-
xytext=(30, -30),
45-
textcoords='offset points',
46-
color='w',
47-
ha='left',
48-
bbox=dict(boxstyle='round,pad=.5', fc=(.1, .1, .1, .92),
49-
ec=(1., 1., 1.), lw=1, zorder=1))
50-
51-
rect_tip = ax.annotate(
52-
'This is a green rectangle.',
53-
xy=(-5, 10),
54-
xytext=(30, 40),
55-
textcoords='offset points',
56-
color='w',
57-
ha='left',
58-
bbox=dict(boxstyle='round,pad=.5', fc=(.1, .1, .1, .92),
59-
ec=(1., 1., 1.), lw=1, zorder=1))
60-
61-
# Set id for the patches
62-
for i, t in enumerate(ax.patches):
63-
t.set_gid('patch_%d' % i)
64-
65-
# Set id for the annotations
66-
for i, t in enumerate(ax.texts):
67-
t.set_gid('tooltip_%d' % i)
34+
rect1 = plt.Rectangle((10, -20), 10, 5, fc='blue')
35+
rect2 = plt.Rectangle((-20, 15), 10, 5, fc='green')
6836

37+
shapes = [rect1, rect2]
38+
labels = ['This is a blue rectangle.', 'This is a green rectangle']
39+
40+
for i, (item, label) in enumerate(zip(shapes, labels)):
41+
patch = ax.add_patch(item)
42+
annotate = ax.annotate(labels[i], xy=item.get_xy(), xytext=(0, 0),
43+
textcoords='offset points', color='w', ha='center',
44+
fontsize=8, bbox=dict(boxstyle='round, pad=.5', fc=(.1, .1, .1, .92),
45+
ec=(1., 1., 1.), lw=1, zorder=1))
46+
47+
ax.add_patch(patch)
48+
patch.set_gid('mypatch_{:03d}'.format(i))
49+
annotate.set_gid('mytooltip_{:03d}'.format(i))
6950

7051
# Save the figure in a fake file object
7152
ax.set_xlim(-30, 30)
@@ -81,16 +62,16 @@
8162
tree, xmlid = ET.XMLID(f.getvalue())
8263
tree.set('onload', 'init(evt)')
8364

84-
# Hide the tooltips
85-
for i, t in enumerate(ax.texts):
86-
el = xmlid['tooltip_%d' % i]
87-
el.set('visibility', 'hidden')
88-
89-
# Assign onmouseover and onmouseout callbacks to patches.
90-
for i, t in enumerate(ax.patches):
91-
el = xmlid['patch_%d' % i]
92-
el.set('onmouseover', "ShowTooltip(this)")
93-
el.set('onmouseout', "HideTooltip(this)")
65+
for i in shapes:
66+
# Get the index of the shape
67+
index = shapes.index(i)
68+
# Hide the tooltips
69+
tooltip = xmlid['mytooltip_{:03d}'.format(index)]
70+
tooltip.set('visibility', 'hidden')
71+
# Assign onmouseover and onmouseout callbacks to patches.
72+
mypatch = xmlid['mypatch_{:03d}'.format(index)]
73+
mypatch.set('onmouseover', "ShowTooltip(this)")
74+
mypatch.set('onmouseout', "HideTooltip(this)")
9475

9576
# This is the script defining the ShowTooltip and HideTooltip functions.
9677
script = """
@@ -104,15 +85,14 @@
10485
}
10586
10687
function ShowTooltip(obj) {
107-
var cur = obj.id.slice(-1);
108-
109-
var tip = svgDocument.getElementById('tooltip_' + cur);
88+
var cur = obj.id.split("_")[1];
89+
var tip = svgDocument.getElementById('mytooltip_' + cur);
11090
tip.setAttribute('visibility',"visible")
11191
}
11292
11393
function HideTooltip(obj) {
114-
var cur = obj.id.slice(-1);
115-
var tip = svgDocument.getElementById('tooltip_' + cur);
94+
var cur = obj.id.split("_")[1];
95+
var tip = svgDocument.getElementById('mytooltip_' + cur);
11696
tip.setAttribute('visibility',"hidden")
11797
}
11898

0 commit comments

Comments
 (0)