1

Trying to implement the following code example as a line plot (mode="lines"), whilst keeping the conditional coloring working. Conditional color formatting does not come through with 'line'.

import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame({
    "x": [1, 2, 3, 4, 5, 6, 7],
    "y": [200, 400, 100, 800, 500, 200, 1100]
})

# Condition
colors = ["blue" if y > 600 else "red" for y in df["y"]]

# Scatter plot with conditional colors
fig = go.Figure(
    go.Scatter(
        x=df["x"],
        y=df["y"],
        mode="markers", # coloring does not work with mode="lines"
        marker=dict(color=colors)
    )
)
fig.show()

I would rather keep it all in one Scatter, as opposed to subplots with make_subplots.

UPDATE when using when using mode="lines" AND plotly 5.11.0, no error message is through, however coloring doesn't come through: enter image description here

I'm trying to make the marked lines red in this case as those markers are greater than 600.

eNath
  • 109
  • 5
  • In my environment (plotly:5.11.0), the graph was drawn correctly with `mode='lines'`. Is the version you are currently using the latest version? – r-beginners Dec 08 '22 at 01:21
  • Hello @r-beginners thank you for the reply, I updated to the most recent plotly so no error message, but it's not having the desired effects (see the above edits). – eNath Dec 08 '22 at 07:55
  • Hello @ChristianKarcher thank you for the reply, I have just updated the question to show the desired output.. if you change to "lines+markers" you'll see the marker points highlight red but not the line. – eNath Dec 08 '22 at 07:56
  • Does this answer your question? [Plotly: How to display different color segments on a line chart for specified thresholds?](https://stackoverflow.com/questions/64760484/plotly-how-to-display-different-color-segments-on-a-line-chart-for-specified-th) – Christian Karcher Dec 08 '22 at 07:56
  • thanks, I saw that, deleted my comment and suggested a possible duplicate ;) – Christian Karcher Dec 08 '22 at 07:58

0 Answers0