Hover template text in a bar graph

I suppose my question is very easy but can’t get the answer.

i have df that looks like that:

               Value Growth Monétaire
2021-01-01      60.    10.     30
2021-02-01      55.    5.     40
2021-03-01      60.    10.     30
2021-04-01      60.    10.     30

I would like to make a plotly graph with bar representing each column in a bar.
I did:

    fig = go.Figure(
        [go.Bar(
            name=col,
            x=style.index,
            y=style[col],
            hovertemplate='<br>'.join(['Classe : %{label}',
                                       'Date: %{x}',
                                       'Poids: %{y:.1f}%',
                                       '<extra></extra>'
                                       ])
            ) 
         for col in style.columns]
        )
    fig.update_layout(showlegend=True,
                      yaxis=dict(range=[0, 100],
                                 ticksuffix='%'),
                      barmode='stack'
                      )
    fig.update_xaxes(dtick="M3",
                     tickformat="%b\n%Y", 
                     )

But I would like the hover template to be for example:
classe : Value
Date: Aug 2021
Poids: 60

I can’t get the “classe” in the hover template ie “Value”, “growth” or “MonĂ©taire”

Hi @Jacques, hovertemplate only has access to x and y and no other data. If you wish to add other data in your hovertemplate you need to define a customdata first, then you’ll be able to call customdata inside your hovertemplate (full doc here).

See your modified code below:

fig = go.Figure(
        [go.Bar(
            name=col,
            x=df.index,
            y=df[col],
            customdata=[col,col, col, col],
            hovertemplate='<br>'.join(['Classe : %{customdata}',
                                       'Date: %{x}',
                                       'Poids: %{y:.1f}%',
                                       '<extra></extra>'
                                       ])
            ) 
         for col in df.columns]
        )
fig.update_layout(showlegend=True,
                    yaxis=dict(range=[0, 100],
                                ticksuffix='%'),
                    barmode='stack'
                    )
fig.update_xaxes(dtick="M3",
                     tickformat="%b\n%Y", 
                     )
1 Like