Skip to content

Files

Latest commit

3627cdc · Jul 12, 2019

History

History
108 lines (89 loc) · 2.61 KB

hover-text-and-formatting.md

File metadata and controls

108 lines (89 loc) · 2.61 KB
jupyter
jupytext kernelspec language_info plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.1
1.1.7
display_name language name
Python 3
python
python3
codemirror_mode file_extension mimetype name nbconvert_exporter pygments_lexer version
name version
ipython
3
.py
text/x-python
python
python
ipython3
3.6.5
description display_as has_thumbnail ipynb language layout name order permalink thumbnail title v4upgrade
How to use hover text and formatting in Python with Plotly.
file_settings
true
~notebook_demo/198
python
user-guide
Hover Text and Formatting
30.5
python/hover-text-and-formatting/
thumbnail/hover-text.png
Hover Text and Formatting | Plotly
true

Hover text with plotly express

Many plotly express functions support configurable hover text. The hover_data argument accepts a list of column names to be added to the hover tooltip. The hover_name property controls which column is displayed in bold as the tooltip title.

Here is an example that creates a scatter plot using plotly express with custom hover data and a custom hover name.

import plotly.express as px

gapminder_2007 = px.data.gapminder().query("year==2007")

fig = px.scatter(gapminder_2007, x="gdpPercap", y="lifeExp", log_x=True,
                 hover_name="country", hover_data=["continent"])

fig.show()

Add Hover Text

import plotly.graph_objects as go

fig = go.Figure()

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

fig.show()

Format Hover Text

import plotly.graph_objects as go


fig = go.Figure(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
))

fig.update_layout(
    title_text=("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>"),
    title_font=dict(
        size=10
    ),
)

fig.update_xaxes(zeroline=False)
fig.update_yaxes(hoverformat=".2f")

fig.show()

Reference

See https://plot.ly/python/reference/ for more information and chart attribute options!