[Bug] Hovertemplate not persisted after animation
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:

After animation:

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.
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 @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
I unfortunately cannot get either of these two workarounds to work using a choropleth or a choropleth_mapbox. Any advice ?
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