Skip to content

customdata/hovertemplate displays literal text with Histogram2d #3419

@Astrodude11

Description

@Astrodude11

My goal is to add custom hover data to a heat map using px.density_heatmap. The documentation provides hover_data but the hovertemplate string does not include the data given in hover_data.

Using the first heat map shown on the Plotly 2D Histogram webpage as an example:

import plotly.express as px


df = px.data.tips()
fig = px.density_heatmap(df, x='total_bill', y='tip', hover_data=['size'])
print(fig.data[0].hovertemplate)

The output of hovertemplate is:

total_bill=%{x}<br>tip=%{y}<br>count=%{z}<extra></extra>

For the sake of comparison, the hovertemplate string using px.scatter does include customdata:

total_bill=%{x}<br>tip=%{y}<br>size=%{customdata[0]}<extra></extra>

Therefore, I tried to use graph_objects to explicitly define customdata on go.Histogram2d to display the desired hover data like so:

import plotly.graph_objects as go
import plotly.express as px


df = px.data.tips()

fig = go.Figure(
    data=go.Histogram2d(
        x=df['total_bill'], 
        y=df['tip'],
        z=df['size'],
        histfunc='sum',
        customdata=[df['smoker']]
    )
)

fig.update_traces(
    hovertemplate='<br>'.join([
        'Bill $: %{x}',
        'Tip $: %{y}',
        'Size: %{z}',
        'Smokes: %{customdata[0]}'
    ])
)

fig.show()

However, as also reported by a user on the Plotly Community Board, the literal text of %{customdata[0]} is displayed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions