Python Charts

Python plotting and visualization demystified

Customize Hover Tooltips in Plotly (hovertemplate)

Customize Plotly hover tooltips with hovertemplate for precise formatting.

TL;DR

Use hovertemplate to define exactly what appears in Plotly hover text, and hoverlabel to style the tooltip box.

import plotly.graph_objects as go

fig = go.Figure(
    data=[
        go.Bar(
            x=['Q1', 'Q2', 'Q3', 'Q4'],
            y=[120, 150, 100, 130],
            hovertemplate='<b>%{x}</b><br>Sales: %{y:$,.0f}<extra></extra>'
        )
    ]
)
fig.show()

What hovertemplate does

hovertemplate lets you build a tooltip string with Plotly variables such as %{x}, %{y}, and %{text}. It also gives you control over the exact formatting and whether extra metadata appears.

  • %{x} and %{y} insert axis values.
  • %{text} inserts any additional text you pass in the trace.
  • <br> adds line breaks.
  • <extra></extra> removes the secondary trace name block.

Basic hovertemplate example

import plotly.graph_objects as go

fig = go.Figure(
    data=[
        go.Scatter(
            x=[1, 2, 3],
            y=[10, 15, 13],
            mode='markers+lines',
            hovertemplate='Point %{x}<br>Value %{y}<extra></extra>',
        )
    ],
    layout=dict(title='Basic hovertemplate example')
)

fig.show()

Format numbers and dates

You can format values directly in hovertemplate using Plotly's formatting syntax.

import plotly.graph_objects as go

fig = go.Figure(
    data=[
        go.Bar(
            x=['Jan', 'Feb', 'Mar'],
            y=[42000, 51000, 47000],
            hovertemplate='<b>%{x}</b><br>Revenue: %{y:$,.0f}<extra></extra>',
        )
    ],
    layout=dict(title='Formatted hovertemplate values')
)

fig.show()

%{y:$,.0f} formats the y value as currency with a comma separator and no decimals.

Add custom hover text

If your trace has a text field, hovertemplate can show it along with other values.

import plotly.graph_objects as go

fig = go.Figure(
    data=[
        go.Bar(
            x=['Apples', 'Oranges', 'Bananas'],
            y=[30, 45, 25],
            text=['Fresh', 'Citrus', 'Tropical'],
            hovertemplate='<b>%{x}</b><br>%{text}<br>Qty: %{y}<extra></extra>',
        )
    ],
    layout=dict(title='Custom hover text example')
)

fig.show()

Style the tooltip box with hoverlabel

Use hoverlabel to customize the tooltip font, background, and border.

import plotly.graph_objects as go

fig = go.Figure(
    data=[
        go.Bar(
            x=['Red', 'Blue', 'Green'],
            y=[23, 17, 28],
            hovertemplate='<b>%{x}</b><br>Count %{y}<extra></extra>',
            hoverlabel=dict(
                bgcolor='#ffffff',
                bordercolor='#264653',
                font=dict(color='#264653', size=13),
            ),
        )
    ],
    layout=dict(title='Hoverlabel styling')
)

fig.show()

Hide the extra trace information

The <extra></extra> snippet removes the default trace name block from the tooltip. Leave it out when you want the legend label to appear.

Use hovermode to control interaction

hovermode can change how Plotly shows hover labels when multiple traces overlap.

fig.update_layout(hovermode='x unified')
fig.show()

This makes it easier to compare values across traces on the same x position.

Quick guideline

  • Use hovertemplate for exact content.
  • Use hoverlabel to style the tooltip.
  • Use <extra></extra> to remove the trace name.
  • Use hovermode='x unified' for combined tooltips across traces.