Skip to content

add ability to hide missing data from hover tooltip #3515

Open

Description

@janosh

I have a dataframe column with some missing values which I want to include in hover_data. Is it possible to show the column in the hover tooltip only if there's value for the currently hovered point? The current default behavior is to show col_name=%{customdata[0]} for missing values. I think it would be better to show nothing at all.

Screen Shot 2021-12-14 at 13 16 13

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")

df["pop"] = None

fig = px.line(
    df,
    x="year",
    y="lifeExp",
    color="country",
    hover_data=["pop"],
    title="layout.hovermode='closest' (the default)",
)
fig.update_traces(mode="markers+lines")

fig.show()

Activity

  1. empet commented on Dec 27, 2021

    @empet

    Commenting out these two lines

    df["pop"] = None
    

    and/or just this one:

    hover_data=["pop"],
    

    removes pop:= %{customdata[0]} from tooltips.

  2. janosh commented on Dec 27, 2021

    @janosh
    ContributorAuthor

    @empet You misunderstood. I do want the population data in the tooltip, just not for points where that data is missing. The line

    df["pop"] = None

    is just to simulate missing data.

  3. empet commented on Dec 27, 2021

    @empet

    @janosh
    I thought you want remove customdata just for this example, because here if you are dsiplaying fig.data[0], and fig.data[1] you'll see that in both cases the elements of customdata are all None:

    'customdata': array([[None],
                             [None],
                             [None],
                             [None],
                             [None],
                             [None],
                             [None],
                             [None],
                             [None],
                             [None],
                             [None],
                             [None]], dtype=object)
    
  4. janosh commented on Dec 27, 2021

    @janosh
    ContributorAuthor

    I know. They're all None because I was too lazy to only set the value for that point to None which is being hovered in the screenshot.

  5. empet commented on Dec 27, 2021

    @empet

    I think that a solution is to convert customdata to strings, and set ' ', instead of None:

    import numpy as np
    import plotly.graph_objects as go
    N=24
    vals = np.random.randint(1000, 2001, N).astype(str)
    #or vals = (7*np.random.rand(24)).astype(str)
                           
    I = np.random.randint(0, N, 12)
    vals[I]=''
    
    fig=go.Figure(go.Scatter(x=np.arange(0,N), y=4+5*np.random.rand(N), customdata=vals,
                  hovertemplate='vals: %{customdata}'))
    fig.show()
    

    Unfortunately we cannot access individual elements in customdata to set hovertemplate with an if else , depending on whether an element is ' ', or not.

  6. changed the title [-]Hide custom data from hover tooltip for missing data[/-] [+]Hide missing data from hover tooltip[/+] on Jan 22, 2022
  7. krieg0102 commented on Aug 18, 2022

    @krieg0102

    Is there any other way to slove the problem? l have a stacked line graph with same question, some point missing value in stack and display %{customdata} in hover. i can not convert customdata to strings, and set ' ', in fact this point even not in my dataframe, just because other point in same stack it appeard lije this:
    image

  8. lijecaru commented on Aug 18, 2022

    @lijecaru

    Is there any development on this issue? I would also like my hovertip to skip that specific customdata if it's np.nan

  9. yaelbh commented on Aug 25, 2022

    @yaelbh

    I also have the same problem. Will be nice if the package owners will give it high priority.

  10. sarangof commented on Mar 27, 2023

    @sarangof

    Ditto.

  11. self-assigned this
    on Jul 11, 2024
  12. removed their assignment
    on Aug 2, 2024
  13. changed the title [-]Hide missing data from hover tooltip[/-] [+]add ability to hide missing data from hover tooltip[/+] on Aug 12, 2024
  14. alexscott-ff commented on Apr 11, 2025

    @alexscott-ff

    The last workaround, and generally filling a dataframe with a string, now throws an error in pandas, and will soon not be allowed, for columns with dtypes that are not strings (datetime, float, etc) I believe. That's just to say that a solution here soonish would be a big value add :)

    I think my ideal preference as far as implementation goes would be to have a layout or trace setting where you could give a value for missing data, e.g. "-", and missing data would be sniffed using pd.isnull because things like NaT will be missed otherwise I think.

  15. gvwilson commented on Apr 11, 2025

    @gvwilson
    Contributor

    I appreciate the need, but realistically, our team isn't going to get to this in the plotting layer any time soon. We'd be happy to prioritize review of a community PR if someone wants to put one together. Thanks - @gvwilson

  16. emilykl commented on Aug 28, 2026

    @emilykl
    Contributor

    As of plotly.py v6.4.0, this situation is handled much better: In the case of missing data, a hyphen is displayed rather than the raw template string (implemented by plotly/plotly.js#7577 in plotly.js). So basically the behavior proposed by @alexscott-ff.

    A different fallback string besides a hyphen can be used by setting the trace's hovertemplatefallback attribute.

    I'll leave this issue open, though, since it's still not possible to hide the hovertext entry entirely when data is missing.

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

    P3backlogfeaturesomething new

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions