0

Sample line chart

import plotly.express as px

df = px.data.stocks()
fig = px.line(df, x='date', y="GOOG")
fig.show()

enter image description here

I want to color the values > 1.1 and values<0.95 to a different color(ex: red)

enter image description here

Is it possible in plotly to color the specific portion of the line into a different color?

Ailurophile
  • 2,552
  • 7
  • 21
  • 46
  • Check out this question: https://stackoverflow.com/questions/64760484/plotly-how-to-display-different-color-segments-on-a-line-chart-for-specified-th – Eog Aug 15 '22 at 09:57

1 Answers1

1
  • you can add a additional traces that are data points outside desired range
  • note pandas techniques to ensure that all data points exist for additional trace (use of outer-join to get NaN for points within range)
import plotly.express as px

df = px.data.stocks()
fig = px.line(df, x="date", y="GOOG").add_traces(
    px.line(
        df.loc[~df["GOOG"].between(0.95, 1.1)].merge(
            df, on="date", how="right", suffixes=("", "_r")
        ),
        x="date",
        y="GOOG",
    )
    .update_traces(line_color="red")
    .data
)

fig.show()

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30