Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ENH]: Data tooltip support #23378

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

Open
timhoffm opened this issue Jul 1, 2022 · 12 comments · May be fixed by #25831
Open

[ENH]: Data tooltip support #23378

timhoffm opened this issue Jul 1, 2022 · 12 comments · May be fixed by #25831
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues New feature

Comments

@timhoffm
Copy link
Member

timhoffm commented Jul 1, 2022

Problem

To better support interactivity and contextual data, we should support data tooltips.

Proposed solution

Things to be done:

  1. Add tooltip API to backend_bases. This should be similar to the picker functionality, only with hover instead of click. T.b.d.: Do we want a TooltipEvent or a more general HoverEvent?
  2. Implement that API for the backends. As a first step or prove of concept one could start with one backend, e.g. Qt and add the other backends later. ipympl could/should follow at some point as well.
  3. Add high-level API to support setting tooltips.
    We likely want at least:
    • explicit strings: plt.plot(x, y, tooltip=['A', 'B', 'C', 'D'])
    • data support: plt.plot('x', 'y', tooltip='label', data=data)
    • functions: plt.plot(x, y, tooltip=lambda x, y: f'({x}, {y})')
@timhoffm timhoffm added New feature Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues labels Jul 1, 2022
@jklymak
Copy link
Member

jklymak commented Jul 1, 2022

Is this different than the existing cursor coord readout? I far prefer a readout in the corner to a data tooltip because it doesn't cover my data more than the mouse already does....

@timhoffm
Copy link
Member Author

timhoffm commented Jul 2, 2022

This is different because you can show arbitrary data, not just the coordinates.

@tacaswell
Copy link
Member

My instinct is a HoverEvent (and an UnHoverEvent?) and add a generic show_tooltip / hide_tooltip methods on the Canvas classes

How does this interact with the existing mouseover / get_cursor_data functionality?

@tacaswell tacaswell added this to the v3.7.0 milestone Jul 3, 2022
@timhoffm
Copy link
Member Author

timhoffm commented Jul 4, 2022

How does this interact with the existing mouseover / get_cursor_data functionality?

T.b.d. I think get_cursor_data is quite limited in its output because it has to fit in a single line in the gui.

I'm looking for things like this:

https://docs.bokeh.org/en/latest/docs/user_guide/tools.html#hovertool
https://plotly.com/python/hover-text-and-formatting/

in particular also the ability to show context information on data points, which get_cursor_data currently does not have.

@story645
Copy link
Member

story645 commented Jul 4, 2022

How does this differ from how https://mplcursors.readthedocs.io/ and https://github.com/joferkington/mpldatacursor implement hovering/would it make those libraries way simpler?

@oscargus
Copy link
Member

This should, btw, also be possible to support in the PDF and SVG backends.

@tacaswell
Copy link
Member

I think we even have a demo of post-processing an SVG to add tooltips.

@timhoffm
Copy link
Member Author

@oscargus
Copy link
Member

Slightly related: #9957

@Dil003
Copy link

Dil003 commented May 14, 2025

Hi there,

Some group members and I are working on implementing tooltip support for this issue, and we're looking for guidance on the most efficient way to handle the high-level API.

Rather than modifying each plotting function individually (e.g., plot(), scatter(), bar(), etc.) to accept a hover parameter, is there a preferred approach for applying this functionality across all plotting functions?

We’re wondering if there’s:

A shared wrapper, internal dispatcher, or base function we could hook into,

A decorator pattern that could be reused,

Or perhaps an existing mechanism (like how data or label are handled) that we could align with?

Our goal is to avoid duplicating logic while ensuring consistent behavior and maintaining backward compatibility. Any suggestions on where best to integrate this or references to similar patterns used in the codebase would be appreciated.

Thanks in advance!

@timhoffm
Copy link
Member Author

timhoffm commented May 15, 2025

You can and probably should add this to the Artist base class - get/set_tooltip. This will be propagated though **kwargs by most plotting functions, just like label. Though you'll likely need per-Artist-subclass specific implementation of data mapping, i.e. what is the tooltip for a given data coordinate.

That said, you don't have to implement all Artists right away. I suggest starting with Line2D. If you manage to to do 1., 2. and a specific version of 3. for Line2D, you've come quite far. We can generalize the high level API subsequently.

@story645
Copy link
Member

story645 commented May 15, 2025

@Dil003 to follow on Tim's comment, you may also want to look at the data cursor mechanism/API folks have been discussing b/c it's got somewhat similar functionality and possibly look at how some of the third party packages implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues New feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants