Black Lives Matter. Please consider donating to Black Girls Code today.

Using default auto assigned color for two traces

Hi all,

I am creating two instances of a scatter plot and appending them to the same graph object and plotting it. Each scatter trace gets an x and a y array as well as the name, every other parameter is default. When they get plotted they get assigned a color each. I want the two scatter plots to be the same color.

I could solve this by creating a dictionary of colors and then choosing a random color and assigning it to both scatter traces but I am curious if there is a way to do it automatically and use default auto assigned color of one trace as the color of another trace.

I tried grouping them into a legend group but it doesn’t give me the color solution.

Below is a simplified code of what I’m doing:

plot_trace1 = go.Scatter(
                        x=xi,
                        y=yi,
                        mode='lines',
                        name=str(i['Depth']),
                        legendgroup =str(i['Depth'])
                    )
                    
plot_trace2 = go.Scatter(
                        x=i['x_pts'],
                        y=ypts,
                        mode='markers',
                        name=str(i['Depth']),
                        legendgroup =str(i['Depth'])
                    )

                    plot_data.append(plot_trace1)
                    plot_data.append(plot_trace2)

plot_layout = {'title': 'Comparison Plot',
                   'legend': dict(x=0, y=-0.5, traceorder='grouped'),
                   'xaxis':dict(
                       title='Position (mm)',
                       tickmode='linear',
                       ticks='outside',
                       dtick=maxx/5,
                       ticklen=8,
                       tickwidth=1,
                       tickcolor='#000'
                   ),
                   'yaxis':dict(
                       title='Reading',
                       tickmode='linear',
                       ticks='outside',
                       dtick=maxy/10,
                       ticklen=8,
                       tickwidth=1,
                       tickcolor='#000'
                   ),          
    }
MC_fig = go.Figure(data=plot_data, layout=plot_layout)

Thanks,
Mike

Did you ever figure out if it is possible to achieve this? I’m in the same situation and would rather use built-in color assignment than creating a separate color list to iterate over.

The default colors of plotly (when using the default template) are plotly.colors.qualitative.Plotly (see https://plot.ly/python/discrete-color/#color-sequences-in-plotly-express), which is a list, so you could assign to your traces colors from this list. You can also change the layout colorway (https://plot.ly/python/reference/#layout-colorway) to impose that the first two traces always have the same color, for example – but again you have to say which color (this solution was suggested in How do I force identical colors for all plots in a subplot?). Other than that I don’t think it’s possible to let plotly choose the color for you but impose that it’s the same color for two traces.

2 Likes

Good enough for me, thanks!