How to change line color according my condition?

This is my code

fig = go.Figure(data=go.Scatter(
    x=grouped_data.index,
    y=grouped_data['avg_sentiment'],
    mode='lines',
    name='Sentiment',
    line=dict(
    color=['red' if val >= 0 else 'green' for val in grouped_data['avg_sentiment']],
    width=2
    )
))

the range of grouped_data[โ€˜avg_sentimentโ€™]] is between -1 to 1
but have error


Please help me!

Hi,

unfortunately, the line_color only accepts a single value.

If you want to have different colors for line segments, you will have to add traces for each segment, here an example:

1 Like

Thank you!!! I finish

1 Like