David
David

Reputation: 379

How to show data points and it's labels at a time while hovering it in line graph in Plotly in Python 3?

I want to add the data points and their respective labels in the line graph in PLOTLY which will visible on hovering it.

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 = True,textposition='top center'
    )
]

py.iplot(data, filename = "add-hover-text")

enter image description here

I did then hoverinfo = 'y', which show like,

enter image description here

I've written the code, but it is showing either data points or the labels defined in the text. I want to show both at a time. Can you help me to show data points and it's labels at a time.

Upvotes: 2

Views: 1202

Answers (1)

DatHydroGuy
DatHydroGuy

Reputation: 1116

Simply change your hoverinfo = 'text', to hoverinfo = 'text+y',

The hoverinfo help states that:

The 'hoverinfo' property is a flaglist and may be specified
as a string containing:
  - Any combination of ['x', 'y', 'z', 'text', 'name'] joined with '+' characters
    (e.g. 'x+y')
    OR exactly one of ['all', 'none', 'skip'] (e.g. 'skip')
  - A list or array of the above

Upvotes: 1

Related Questions