🏥 🏭 Working on the COVID-19 response in Canada? Plotly & the Canadian government can help you and your organization. Learn more and get in touch.

Hover Text and Formatting in Python/v3

How to use hover text and formatting in Python with Plotly.


Note: this page is part of the documentation for version 3 of Plotly.py, which is not the most recent version.
See our Version 4 Migration Guide for information about how to upgrade.
The version 4 version of this page is here.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [1]:
import plotly
plotly.__version__
Out[1]:
'3.9.0'

Add Hover Text

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [
    go.Scatter(
        x = [1,2,3,4,5],
        y = [2,1,6,4,4],
        text = ["Text A", "Text B", "Text C", "Text D", "Text E"],
        hoverinfo = 'text',
        marker = dict(
            color = 'green'
        ),
        showlegend = False
    )
]

py.iplot(data, filename = "add-hover-text")
Out[2]:

Format Hover Text

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

data = [
    go.Scatter(
        x = [1,2,3,4,5],
        y = [2.02825,1.63728,6.83839,4.8485,4.73463],
        hoverinfo = 'y',
        marker = dict(
            color = 'green'
        ),
        showlegend = False
    )
]

layout = go.Layout(
    title = "Set hover text formatting<br><a href= https://github.com/d3/d3-time-format/blob/master/README.md#locale_format>https://github.com/d3/d3-time-format/blob/master/README.md#locale_format</a>",
    titlefont = dict(
        size = 10
    ),
    xaxis = dict(
        zeroline = False
    ),
    yaxis = dict(
        hoverformat = '.2f'
    )
)

fig = go.Figure(data=data,layout=layout)
py.iplot(fig, filename = "format-hover-text")
Out[3]:

Hovertemplate

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.io as pio


data = [
    go.Scatter(
        x = [1,2,3,4,5],
        y = [2.02825,1.63728,6.83839,4.8485,4.73463],
        hovertemplate = '<i>Price</i>: $%{y:.2f}'
                        '<br><b>X</b>: %{x}<br>'
                        '<b>%{text}</b>',
        text = ['Custom text {}'.format(i + 1) for i in range(5)],
        showlegend = False
    ),
    go.Scatter(
        x = [1,2,3,4,5],
        y = [3.02825,2.63728,4.83839,3.8485,1.73463],
        hovertemplate = 'Price: %{y:$.2f}<extra></extra>',
        showlegend = False
    )
]

layout = go.Layout(
    title = "Set hover text with hovertemplate",
    template = pio.templates['plotly'],
)

fig = go.Figure(data=data,layout=layout)
py.iplot(fig, filename = "hovertemplate-basic")
Out[4]:

Advanced Hovertemplate

In [5]:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.io as pio

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/job-automation-probability.csv')

data = [
    dict(
        type = 'scatter',
        mode = 'markers',
        x = dff['prob'],
        y = dff['Average annual wage'],
        text = dff['short occupation'],
        name = education_level,
        marker = dict(size = dff['numbEmployed'], sizeref = 4000, sizemode = 'area'),
        hovertemplate = "<b>%{text}</b><br><br>" +
            "%{yaxis.title.text}: %{y:$,.0f}<br>" +
            "%{xaxis.title.text}: %{x:.0%}<br>" +
            "Number Employed: %{marker.size:,}" +
            "<extra></extra>"
    ) for dff, education_level in [(df[df.education == education_level], education_level) for education_level in df.education.unique()]
]

layout = go.Layout(
    title = "Higher Risk of Job Automation in Lower Paying Jobs",
    template = pio.templates['plotly'],
    legend = dict(orientation = 'h', y = -0.3),
    xaxis = dict(title = 'Automation Probability'),
    yaxis = dict(title = 'Income')
)

fig = dict(data=data,layout=layout)
py.iplot(fig, filename = "hovertemplate-advanced")
Out[5]: