Plotly: Is it possible to color specific portion of the line in line chart?
Question:
Answers:
- 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()
- 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()