NAS_2339
NAS_2339

Reputation: 353

Adding text to each point in plotly scatter plot

I am creating a scatter plot with the below code.

trace3 =  go.Scatter(   
    x=tmp_attr.sort_index().index,
    y=tmp_attr.sort_index().values,
    yaxis = 'y2',
    name='% Buy', opacity = 0.6,
    marker=dict(
        color='black',
        line=dict(color='#000000',
                  width=2 )

    )

How do I add text corresponding to the y value for each (x,y) pair in the graph?

Upvotes: 0

Views: 898

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31226

Simply define text and mode

    text=[f"{v:.2f}" for v in tmp_attr.sort_index().values],
    mode="text+lines"

full MWE

import plotly.graph_objects as go
import pandas as pd
import numpy as np

# simulate some data
tmp_attr = pd.Series(np.sin(np.linspace(0, 6.2, 15)))

trace3 = go.Scatter(
    x=tmp_attr.sort_index().index,
    y=tmp_attr.sort_index().values,
    yaxis="y2",
    name="% Buy",
    opacity=0.6,
    marker=dict(color="black", line=dict(color="#000000", width=2)),
    text=[f"{v:.2f}" for v in tmp_attr.sort_index().values],
    mode="text+lines"
)

go.Figure(trace3)

Upvotes: 1

Related Questions