go.Scatter - add to text more columns

I would like to add to the “text” more columns than one.

Here is how i added one column to the text:

fig.add_trace(go.Scatter(x=df1[‘Date’], y=df1[‘Output’], mode=‘lines+markers’, text=df1[‘Colorgroup’]),
row=1, col=1)

How can i add more columns to the text?

@pitS

You did not give details on how you use the two or more columns in the text definition. So I created a dummy example:

import plotly.graph_objects as go


import pandas as pd
d = {'col1': ['aaaa', 'bbb', 'ccccccc', 'dd'],
      'col2': ['absc', 'erwqi', 'nonon', 'olala'],
      'col3': ['qwerty', 'uiop', 'dsaf', 'xyz']}
df = pd.DataFrame(d)

text = [f'info1: {string1}<br>info2: {string2}<br>info3: {string3}'
        for string1, string2, string3 in zip(df['col1'], df['col2'], df['col3'])]

fig= go.Figure(go.Scatter(x=[1,2,3,4], y=[-1, 1.5, 2.75, 2], mode='lines+markers', text=text))
fig.show()

But instead of text you can use customdata and hovertemplate:

customdata= np.stack((df['col1'], df['col2'], df['col3']), axis=-1)
fig = fig= go.Figure(go.Scatter(x=[1,2,3,4], y=[-1, 1.5, 2.75, 2], mode='lines+markers', customdata=customdata,
                                hovertemplate = 'info1: %{customdata[0]}<br>info2: %{customdata[1]}<br>info3: %{customdata[2]}'))
fig.show()

For more details on customdata and hovertemplate definition see this notebook:
[https://chart-studio.plotly.com/~empet/15366/](https://chart-studio.plotly.com/~empet/15366}

1 Like

Dear empet, wow, it works great!
Thank you for your time and effort.