8000 Update svg_tooltip.py by DaveL17 · Pull Request #8324 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 6 commits into from
Apr 16, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update svg_tooltip.py
Original example supported a maximum of three patches. Proposed change should support any number of patches.
  • Loading branch information
DaveL17 authored Mar 18, 2017
commit f25af11262f6c4456131fa18ed3dfa0dfed5af53
71 changes: 25 additions & 46 deletions examples/user_interfaces/svg_tooltip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Member

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?

Copy link
Contributor Author
@DaveL17 DaveL17 Mar 19, 2017

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.

Copy link
Member

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 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.

Copy link
Contributor Author

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.)

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for i, (item, label) in enumerate(zip(shapes, labels)):

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be aligned with dict(.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand All @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y appears unused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hang on a sec. It appears that tooltip = xmlid['mytooltip_%d' % i] throws a TypeError if y is not used.

Disagreed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're doing something wrong then; y should not be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 = """
Expand All @@ -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")
}

Expand Down
0