How do I synchronize colors across trace objects?

Hello,

I’m trying to show a time series plot that has lines for forecasted sales and markers for actual sales. I’m trying to color both by a sales location type (only two - home delivery and shop sales) which correspond to both. However, my forecasted sales will not take a column for the colors like my actual sales do. See below.

How do I get the legend and colors to apply to both forecasted and actual sales?

Below is my code.

fig = px.line(df1, x='dmand_yr_mo', y='sales_dollars', color = 'location_type',
    title="Time Series Look")

    fig.add_trace(
        go.Scatter(mode="markers",
            x=df1['dmand_yr_mo'],
            y=df1['predicted_sales'],
            name="Forecasted Sales",
        )
    )
    fig.update_layout(hovermode="x unified")

HI @jbh, I guess you want the markers for the predicted_sales to be blue and green, depending on your location_type?

Do you have this information? If so, you could add the markers for each location_type separately and apply the color you would like to have.

Right now all your predicted points are plotted as one trace, therefore the uniform color. You could also color each marker manually by a list of colors but I would not recommend this in this case.

Thank you. I just figured it out with some extra help…

fig = px.line(df1, x='dmand_yr_mo', y='sales_dollars', color = 'location_type',
    title="Time Series Look")

    for trace in fig.data:
        c = trace['name']
        trace_color = trace['line']['color']
        fig.add_trace(
            go.Scatter(mode="markers",
                x=df1['dmand_yr_mo'],
                y=df1['predicted_sales'],
                name=c,
                marker=dict(color=trace_color)
            )
        )
        fig.update_layout(hovermode="x unified")
1 Like