'How to set certain color in plotly

I have this kind of data, I want to define color of lines. Here both of the lines are blue(see attached file).

import pandas as pd
import plotly.express as px
import numpy as np
qq = pd.DataFrame(
    {
        "Predicted": np.sort(np.random.uniform(3, 15, 4)),
        "real": np.sort(np.random.uniform(3, 15, 4)),
        "Category": ["A", "B", "C", "D"],
        "new_val": np.random.uniform(3, 15, 4),
    }
)

px.bar(
    qq, x="Category", y=["Predicted", "real", "new_val"], title="Long-Form Input"
).add_traces(
    px.line(qq, x="Category", y="real").update_traces(showlegend=True, name="real").data
).add_traces(
    px.line(qq, x="Category", y="Predicted").update_traces(showlegend=True, name="Predicted").data
)

enter image description here

I am trying to set color='red' and this kind of arguments, but this does not work



Solution 1:[1]

Using color="red" as an argument to line() won't work because the value is expected to be the name of a column in your data frame or a collection of colours per value.

https://plotly.com/python-api-reference/generated/plotly.express.line.html

If you don't want to provide these additional colour values, use line_color="red" as an argument to the update_traces() call.

Solution 2:[2]

The data is displayed in fig.data, so the line graph can be rewritten directly from the scatter plots that already exist.

fig = px.bar(qq, x="Category", y=["Predicted", "real", "new_val"], title="Long-Form Input")

fig.data[3].line.color = 'red'
fig.data[4].line.color = 'red'

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 miqh
Solution 2