-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Example for using cursor_data #12451
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
=========== | ||
Cursor Data | ||
=========== | ||
|
||
This example demonstrates how to monkeypatch `.Artist.get_cursor_data` and | ||
`.Artist.format_cursor_data` to show the elements under the cursor in the | ||
status bar of the plot window. | ||
|
||
.. image:: ../../_static/cursor_data.png | ||
|
||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.lines import Line2D | ||
|
||
|
||
def lines_under_cursor(self, event): | ||
""" | ||
Return a list of lines under the cursor for the given MouseMove event. | ||
""" | ||
if not event.inaxes: | ||
return [] | ||
return [line for line in event.inaxes.lines if line.contains(event)[0]] | ||
|
||
|
||
def format_lines(self, data): | ||
"""Convert a list of lines to a comma-separted string.""" | ||
return ', '.join("Line('%s')" % line.get_label() for line in data) | ||
|
||
|
||
Line2D.mouseover = True | ||
Line2D.get_cursor_data = lines_under_cursor | ||
Line2D.format_cursor_data = format_lines | ||
|
||
fig, ax = plt.subplots() | ||
ax.plot([1, 3, 2], label="Label 1", lw=3) | ||
ax.plot([2, 1, 3], label="Label 2", lw=3) | ||
|
||
plt.show() | ||
|
||
|
||
############################################################################# | ||
# | ||
# ------------ | ||
# | ||
# References | ||
# """""""""" | ||
# | ||
# The use of the following functions, methods, classes and modules is shown | ||
# in this example: | ||
|
||
import matplotlib | ||
matplotlib.artist.Artist.get_cursor_data | ||
matplotlib.artist.Artist.format_cursor_data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'd rather you monkeypatch just the line instances that get returned by
ax.plot
; monkeypatching the classes themselves is a bit too global for me :)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.
That would add the trouble of binding a method to an existing object. It makes it even more complicated to understand what's going on:
Of course, you could bind a pure function in this case because
self
is not used, but that would lose generality.Still not convinced that this monkey-patching is actually recommendable.
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.
Indeed, I would personally just bind free functions that don't take self as first argument. Doing it as suggested in your example is definitely overkill (as a side note, you can do
line.get_cursor_data = get_cursor_data.__get__(type(line), line)
which is a bit simpler IMO for the cases where you need it, or even just bind a partial() object... (there isn't much to gain by exactly binding a bound method)).I'm not going to lose sleep over this specific example either way.
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.
Would it be more appropriate to subclass, such as:
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.
hehe...
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.
FWIW this is how I would do it: