Skip to content

[ENH]: Data tooltip support #23378

Open

Description

@timhoffm

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})')

Activity

  1. jklymak commented on Jul 1, 2022

    @jklymak
    Member

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

  2. timhoffm commented on Jul 2, 2022

    @timhoffm
    MemberAuthor

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

  3. tacaswell commented on Jul 3, 2022

    @tacaswell
    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?

  4. added this to the v3.7.0 milestone on Jul 3, 2022
  5. timhoffm commented on Jul 4, 2022

    @timhoffm
    MemberAuthor

    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.

  6. story645 commented on Jul 4, 2022

    @story645
    Member

    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?

  7. oscargus commented on Jul 15, 2022

    @oscargus
    Member

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

  8. tacaswell commented on Jul 16, 2022

    @tacaswell
    Member

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

  9. oscargus commented on May 28, 2023

    @oscargus
    Member

    Slightly related: #9957

  10. Dil003 commented on May 14, 2025

    @Dil003

    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!

  11. timhoffm commented on May 15, 2025

    @timhoffm
    MemberAuthor

    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.

  12. story645 commented on May 15, 2025

    @story645
    Member

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesNew feature

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions