Skip to content

Document Artist.get_cursor_data - #12427

Merged
timhoffm merged 1 commit into
matplotlib:masterfrom
timhoffm:doc-cursor-data
Oct 7, 2018
Merged

timhoffm merged 1 commit into
matplotlib:masterfrom
timhoffm:doc-cursor-data

Conversation

@timhoffm

@timhoffm timhoffm commented Oct 7, 2018

Copy link
Copy Markdown
Member

PR Summary

Closes #12279.

We might additionally consider making get_cursor_data and format_cursor_data private. I don't see a use-case for the end-user.

@ImportanceOfBeingErnest

Copy link
Copy Markdown
Member

Similar to monkey-patching .format_coords (as in the image_zcoord example) a user might also monkey-patch either of get_cursor_data and format_cursor_data. This is actually a bit harder to understand, but allows more flexibility. E.g. the event passed to get_cursor_data carries more information than just a position, which a user can utilize for their puurposes. There is hence value in keeping those methods public.

The actual changes here look fine.

@timhoffm

timhoffm commented Oct 7, 2018 •

Copy link
Copy Markdown
Member Author

@ImportanceOfBeingErnest Maybe a bit beyond this PR, but for monkey-patching we must also specify what event is. Otherwise, the user cannot know what to do in the patched function.

I'm not clear about this myself. Is this just called with MouseEvent or can there be other event types? Do we do this in practice in the current version, or is it just possible that we call it with other events in the future. These things are important to specify if we really want this to be public API. I don't feel competent to judge this and therefore was considering to make this private to not make any false promises.

Do we know if there are really people using this API?

anntzer
anntzer previously requested changes Oct 7, 2018

@anntzer anntzer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

feel free to dismiss once comment handled (even if just to say "let's keep it as it is")

Comment thread lib/matplotlib/artist.py Outdated

.. note::
This is part of the internal API. As an end-user of Matplotlib
you will most likely not call this method yourself.

@anntzer anntzer Oct 7, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't particularly like the wording here, there are in fact a lot of other "public" parts of the Matplotlib API that are "intended as internal/private" (even more than this API)... I guess I'd prefer just something like "intended to be overridden by artist subclasses/monkeypatched" two paragraphs below.

@timhoffm
timhoffm dismissed anntzer’s stale review October 7, 2018 16:23

Requested change handled.

Comment thread lib/matplotlib/artist.py Outdated
likely not call this method yourself.

The default implementation converts ints and floats and arrays of ints
and floats into a comma-separated string.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

comma-separated string, enclosed in square brackets

@tacaswell tacaswell added this to the v3.1 milestone Oct 7, 2018

@tacaswell tacaswell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@timhoffm can self-merge on CI pass.

@timhoffm
timhoffm merged commit 4b1e79f into matplotlib:master Oct 7, 2018
@timhoffm
timhoffm deleted the doc-cursor-data branch October 7, 2018 18:41
@timhoffm

timhoffm commented Oct 7, 2018

Copy link
Copy Markdown
Member Author

Anyone knows something about the event?

#12427 (comment)

@ImportanceOfBeingErnest

Copy link
Copy Markdown
Member

Do we know if there are really people using this API?

From now on, everyone running the below code, is using this 😁

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

def strlinedata(self, event):
    lines = []
    if event.inaxes:
        for line in event.inaxes.lines:
            if line.contains(event)[0]:
                lines.append(line)
    if lines:
        return [line.get_label() for line in lines]
    
def fmtdata(self, data):
    return data.__repr__()

Line2D.mouseover = True
Line2D.get_cursor_data = strlinedata
Line2D.format_cursor_data = fmtdata


fig, ax = plt.subplots()
ax.plot([1,3,2], label="Label 1")
ax.plot([1,2,1], label="Label 2")

plt.show()

image


Anyone knows something about the event?

Implicitely, it needs to be a MouseEvent. This is because mouse_move() calls Artist.contains with the event given to it for every artist that has the mousemove attribute.
The signature for Artist.contains is

def contains(self, mouseevent):
        """Test whether the artist contains the mouse event."""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants