-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Update svg_tooltip.py #8324
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update svg_tooltip.py #8324
Changes from 1 commit
f25af11
9c305d9
83e6395
74aeb10
bcd1e1c
0f86d25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Original example supported a maximum of three patches. Proposed change should support any number of patches.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,41 +31,22 @@ | |
fig, ax = plt.subplots() | ||
|
||
# Create patches to which tooltips will be assigned. | ||
circle = plt.Circle((0, 0), 5, fc='blue') | ||
rect = plt.Rectangle((-5, 10), 10, 5, fc='green') | ||
|
||
ax.add_patch(circle) | ||
ax.add_patch(rect) | ||
|
||
# Create the tooltips | ||
circle_tip = ax.annotate( | ||
'This is a blue circle.', | ||
xy=(0, 0), | ||
xytext=(30, -30), | ||
textcoords='offset points', | ||
color='w', | ||
ha='left', | ||
bbox=dict(boxstyle='round,pad=.5', fc=(.1, .1, .1, .92), | ||
ec=(1., 1., 1.), lw=1, zorder=1)) | ||
|
||
rect_tip = ax.annotate( | ||
'This is a green rectangle.', | ||
xy=(-5, 10), | ||
xytext=(30, 40), | ||
textcoords='offset points', | ||
color='w', | ||
ha='left', | ||
bbox=dict(boxstyle='round,pad=.5', fc=(.1, .1, .1, .92), | ||
ec=(1., 1., 1.), lw=1, zorder=1)) | ||
|
||
# Set id for the patches | ||
for i, t in enumerate(ax.patches): | ||
t.set_gid('patch_%d' % i) | ||
|
||
# Set id for the annotations | ||
for i, t in enumerate(ax.texts): | ||
t.set_gid('tooltip_%d' % i) | ||
rect1 = plt.Rectangle((10, -20), 10, 5, fc='blue') | ||
rect2 = plt.Rectangle((-20, 15), 10, 5, fc='green') | ||
|
||
shapes = [rect1, rect2] | ||
labels = ['This is a blue rectangle.', 'This is a green rectangle'] | ||
|
||
for i, item in enumerate(shapes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. |
||
patch = ax.add_patch(item) | ||
annotate = ax.annotate(labels[i], xy=item.get_xy(), xytext=(0, 0), | ||
textcoords='offset points', color='w', ha='center', | ||
fontsize=8, bbox=dict(boxstyle='round, pad=.5', fc=(.1, .1, .1, .92), | ||
ec=(1., 1., 1.), lw=1, zorder=1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be aligned with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. |
||
|
||
ax.add_patch(patch) | ||
patch.set_gid('mypatch_%d' % i) | ||
annotate.set_gid('mytooltip_%d' % i) | ||
|
||
# Save the figure in a fake file object | ||
ax.set_xlim(-30, 30) | ||
|
@@ -81,16 +62,14 @@ | |
tree, xmlid = ET.XMLID(f.getvalue()) | ||
tree.set('onload', 'init(evt)') | ||
|
||
# Hide the tooltips | ||
for i, t in enumerate(ax.texts): | ||
el = xmlid['tooltip_%d' % i] | ||
el.set('visibility', 'hidden') | ||
|
||
# Assign onmouseover and onmouseout callbacks to patches. | ||
for i, t in enumerate(ax.patches): | ||
el = xmlid['patch_%d' % i] | ||
el.set('onmouseover', "ShowTooltip(this)") | ||
el.set('onmouseout', "HideTooltip(this)") | ||
for i, y in enumerate(shapes): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hang on a sec. It appears that Disagreed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're doing something wrong then; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed this concern in the latest commit. I think all identified issues have been cleared. |
||
# Hide the tooltips | ||
tooltip = xmlid['mytooltip_%d' % i] | ||
tooltip.set('visibility', 'hidden') | ||
# Assign onmouseover and onmouseout callbacks to patches. | ||
mypatch = xmlid['mypatch_%d' % i] | ||
mypatch.set('onmouseover', "ShowTooltip(this)") | ||
mypatch.set('onmouseout', "HideTooltip(this)") | ||
|
||
# This is the script defining the ShowTooltip and HideTooltip functions. | ||
script = """ | ||
|
@@ -106,13 +85,13 @@ | |
function ShowTooltip(obj) { | ||
var cur = obj.id.slice(-1); | ||
|
||
var tip = svgDocument.getElementById('tooltip_' + cur); | ||
var tip = svgDocument.getElementById('mytooltip_' + cur); | ||
tip.setAttribute('visibility',"visible") | ||
} | ||
|
||
function HideTooltip(obj) { | ||
var cur = obj.id.slice(-1); | ||
var tip = svgDocument.getElementById('tooltip_' + cur); | ||
var tip = svgDocument.getElementById('mytooltip_' + cur); | ||
tip.setAttribute('visibility',"hidden") | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change from the
Circle
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to find a clean way to iterate the coordinates of a circle in the example for loop. That's the only reason. Two rectangles made the example straightforward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. Using a
CirclePolygon
, you can use the.xy
property of both, but that requires an additional import frommatplotlib.patches
and it's not really a perfect circle.With
Circle
, you can use the.center
property, but then they're not exactly the same.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. For the example, it didn't seem terribly pertinent that the shapes had to be different (I reckon other than to show that it works with different types of patches.)