|
31 | 31 | fig, ax = plt.subplots() |
32 | 32 |
|
33 | 33 | # 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') |
68 | 36 |
|
| 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)) |
69 | 50 |
|
70 | 51 | # Save the figure in a fake file object |
71 | 52 | ax.set_xlim(-30, 30) |
|
81 | 62 | tree, xmlid = ET.XMLID(f.getvalue()) |
82 | 63 | tree.set('onload', 'init(evt)') |
83 | 64 |
|
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)") |
94 | 75 |
|
95 | 76 | # This is the script defining the ShowTooltip and HideTooltip functions. |
96 | 77 | script = """ |
|
104 | 85 | } |
105 | 86 |
|
106 | 87 | 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); |
110 | 90 | tip.setAttribute('visibility',"visible") |
111 | 91 | } |
112 | 92 |
|
113 | 93 | 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); |
116 | 96 | tip.setAttribute('visibility',"hidden") |
117 | 97 | } |
118 | 98 |
|
|
0 commit comments