Update svg_tooltip.py#8324
Conversation
Original example supported a maximum of three patches. Proposed change should support any number of patches.
| # 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') |
There was a problem hiding this comment.
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.
Interesting. Using a CirclePolygon, you can use the .xy property of both, but that requires an additional import from matplotlib.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.
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.)
| 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.
This should be aligned with dict(.
| 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.
Hang on a sec. It appears that tooltip = xmlid['mytooltip_%d' % i] throws a TypeError if y is not used.
Disagreed.
There was a problem hiding this comment.
You're doing something wrong then; y should not be necessary.
There was a problem hiding this comment.
Addressed this concern in the latest commit. I think all identified issues have been cleared.
| 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.
for i, (item, label) in enumerate(zip(shapes, labels)):
|
(fixes #8316) |
|
Just a note that the script works while patches are 10 or less. When the 11th patch is applied, the SVG will display the tooltip associated with the first patch. I suspect it's the |
- Last commit would fail on the 11th patch. This commit should support an "unlimited" number of patches.
- Updates for {} formatting.
- Edits save file location to remove link to user-specific path erroneously included in last commit.
Addresses the 'y appears unused' issue.
| el = xmlid['patch_%d' % i] | ||
| el.set('onmouseover', "ShowTooltip(this)") | ||
| el.set('onmouseout', "HideTooltip(this)") | ||
| for i in enumerate(shapes): |
There was a problem hiding this comment.
for i in range(len(shapes)); don't need the enumerate.
There was a problem hiding this comment.
Agreed. Commit now references index instead.
References the index of the shape object instead of the enumeration.
QuLogic
left a comment
There was a problem hiding this comment.
Feel free to rebase and squash.
| @@ -1,3 +1,6 @@ | |||
| #! /usr/bin/env python | |||
There was a problem hiding this comment.
It's not executable, so I don't think we need this (and I don't think it should be made executable either.)
| # -*- coding: utf-8 -*- | ||
|
|
||
| """ | ||
| SVG tooltip example |
There was a problem hiding this comment.
Could you add a row of === above this to make the title sphinx-gallery compliant?
|
Thanks @DaveL17 🎉 Hopefully we will here from you again soon! |
|
backported to v2.0.x as 41bd991 |
Original example supported a maximum of three patches. Proposed change should support any number of patches.