plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

[Bug] Hovertemplate not persisted after animation

Open ogelin opened this issue 5 years ago • 4 comments

I've noticed this issue using Plotly with Dash & also reproduced in my own Jupyter notebook.

Here is a code sample reproducing the issue :

import plotly.express as px
df = px.data.gapminder()

fig = px.scatter(df, x="gdpPercap", y="lifeExp",
	         size="pop", color="continent", animation_frame='year', animation_group='country',
                 hover_name="country", log_x=True, size_max=60)

fig.update_traces(
        hovertemplate='<span>hi!!</span>',
    )

fig.show()

When the figure is initially loaded, the tooltips show my custom hover template. After the animation, my hover template is no longer in use and the tooltips show their initial content (i.e. the property names followed by an equal sign & their value).

Before animation: image

After animation: image

ogelin avatar Aug 24 '20 13:08 ogelin

This is indeed surprising and annoying, but update_traces doesn't update traces in the animation frames at the moment, although it probably should! To do this today you will need to iterate across the traces in fig.frames and set the hovertemplate manually.

nicolaskruchten avatar Aug 24 '20 13:08 nicolaskruchten

Thanks @nicolaskruchten, this worked for me:

CUSTOM_HOVERTEMPLATE = "<b>%{customdata[0]}</b><br>%{customdata[1]}<br>%{customdata[2]}"
fig.update_traces(hovertemplate=CUSTOM_HOVERTEMPLATE)
for frame in fig.frames:
    frame.data[0].hovertemplate = CUSTOM_HOVERTEMPLATE

MaxGhenis avatar Aug 04 '21 05:08 MaxGhenis

Thanks @nicolaskruchten, this worked for me:

CUSTOM_HOVERTEMPLATE = "<b>%{customdata[0]}</b><br>%{customdata[1]}<br>%{customdata[2]}"
fig.update_traces(hovertemplate=CUSTOM_HOVERTEMPLATE)
for frame in fig.frames:
    frame.data[0].hovertemplate = CUSTOM_HOVERTEMPLATE

Thanks @MaxGhenis! This worked for me too. But, in my case, I needed to iterate through frame.data.

My code looked like this:

fig.update_traces(hovertemplate=CUSTOM_HOVERTEMPLATE)

for frame in fig.frames:
    for data in frame.data:
        data.hovertemplate = CUSTOM_HOVERTEMPLATE

DaniloSI avatar Dec 22 '21 19:12 DaniloSI

I unfortunately cannot get either of these two workarounds to work using a choropleth or a choropleth_mapbox. Any advice ?

chriscampana avatar Sep 13 '22 22:09 chriscampana

Hi - we are trying to tidy up the stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for several years, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. Thanks for your help - @gvwilson

gvwilson avatar Jul 04 '24 13:07 gvwilson