Description
Following this forum thread: https://community.plotly.com/t/customizing-text-on-x-unified-hovering/39440
Other examples from the forum:
- https://community.plotly.com/t/adapt-name-in-hovertemplate-of-stacked-bar-chart-when-hovermode-x-unified/47825
- https://community.plotly.com/t/updating-hovertemplate-with-hovermode-x-unified/76639
The default value for the heading (first line of the hover content)is x value in "x unified" and y value in "y unified". It is already possible to customize the format of the x/y value by using xhoverformat
and yhoverformat
, respectively:
fig.update_traces(xhoverformat="%b") # for x unified
fig.update_traces(yhoverformat="Month: %b") # for y unified
Ref: https://plotly.com/python/hover-text-and-formatting/#hover-templates-with-mixtures-of-period-data
In the forum thread, a workaround is shared for other scenarios: https://community.plotly.com/t/customizing-text-on-x-unified-hovering/39440/26
This workaround requires adding a new trace with the information that the developer wants to show hardcoded:
hov_text= [f'🌀 Wind Speed <br><br>{d}<br>Daily average: {w} km/h'
for d, w in zip(dff['date'].dt.strftime('%B %d,%Y'), dff['wind_daily_average'])]
fig = go.Figure()
fig.add_scatter(hovertext=hov_text, hoverinfo='text', ...)
The requested feature would allow the developer to specify the variable or value directly without needing to manually create the text for each x value. It could look like this:
# remove title
fig.update_layout(hovertitle = dict(text=None))
# same title for every x value
fig.update_layout(hovertitle = dict(text="Always the same title"))
# set it to a df variable that is not the x value, or that is a combination of values (similar to the current workaround)
fig.update_layout(hovertitle = dict(text=df["my_custom_value"]))
fig.update_layout(hovertitle = dict(text=f'Max stock price in period: {df["max_by_quarter"]}'))