Set the color of the charts inside the subplot

Hi all.
There is a task to make several columns with data, and then make a dash next to them - which would mark the target value.
I tried and I got this:

df = pd.DataFrame({
"Code":['One'],
"last_month": [2],
"Y2022": [1],
"Y2023":[2],
"TG2023":[3.5]
})

fig = make_subplots(rows=1, cols=1)
fig.add_bar(x=df["Code"], y=df[df.columns[1]], text=df[df.columns[1]], row=1, col=1, name="Last month")
fig.add_bar(x=df["Code"], y=df["Y2022"], text=df["Y2022"], row=1, col=1, name="Y2022")
fig.add_bar(x=df["Code"], y=df["Y2023"], text=df["Y2023"], row=1, col=1, name="Y2023")
fig.add_trace(go.Scatter(mode='markers', x=df["Code"], y=df["TG2023"], marker_symbol="line-ew", marker=dict(color='Red', line=dict(color='Red', width=3)), name = "Target 2023"))

image

And it’s almost good. There was a small detail left - to paint the columns in corporate colors.
Usually, when plotting in plotly express, I use the ‘color_discrete_map=colors’ parameter, but this does not work here. I can’t paste this into add_bar or subplot options.

How to correctly set colors for each of the series in grb?

Hi,
Note that if you have a corporate color palette, you can define your own template from these colors, and set a custom colorway, so that every chart designed with either px.express or plotly.graphic_objects use this colorway.

Details are here:

Regarding your trace, if you use pltoly.graphic_objects as go, then go.Figure(), go.Bar(), you can set the color of the bar via marker.

In the example you provided above, you did not specify any color to your .add_bar traces.

ANd you have created a figure with 1 row and 1 col, so, 1 chart. Your 4 traces share the same xaxis. You don’t need to make any subplot for this.

Have a look on the doc here:

I’m sure I’ll look for an answer again later, so I’ll save the solution right here if no one minds.

Assemble into a subplot, and then change the color did not work. Instead, I made a fig to which I added graphs, specifying a color for each.

fig_proc = go.Figure()
fig_proc.add_trace(
    go.Bar(
        x=df_proc["Code"],
        y=df_proc[df_proc.columns[1]],
        text=df_proc[df_proc.columns[1]],
        name = "Last month",
        marker_color='rgb(228,108,10)'
        )
    )
fig_proc.add_trace(
    go.Bar(
        x=df_proc["Code"],
        y=df_proc["Y2022"],
        text=df_proc["Y2022"],
        name = "Y2022",
        marker_color='rgb(142,180,227)'
        )
    )
fig_proc.add_trace(
    go.Bar(
        x=df_proc["Code"],
        y=df_proc["Y2023"],
        text=df_proc["Y2023"],
        name = "Y2022",
        marker_color='rgb(23,56,94)'
        )
    )
fig_proc.add_trace(
    go.Scatter(
        mode='markers',
        x=df_proc["Code"],
        y=df_proc["TG2023"],
        marker_symbol="line-ew",
        marker=dict(
            color='Red',
            #size=120,
            line=dict(
                color='Red',
                width=3
            )
        ),
        name = "Target 2023",
        showlegend=True
    )
)