Search code examples
chartsplotlybar-chartplotly-pythonsubplot

set the same palette for multiple charts in subplot


I want to have 4 charts on a subplot with similar colourings, as the bars are representing the same value. However, the output is giving different colour schemes. What can I do to fix it?

The code:

import plotly
# Creating subplot
fig = make_subplots(rows=2, cols=2, shared_yaxes=True)

cols = plotly.colors.DEFAULT_PLOTLY_COLORS

# Adding charts
fig.add_trace(fig1['data'][0], row=1, col=1)
fig.add_trace(fig1['data'][1], row=1, col=1)
fig.add_trace(fig1['data'][2], row=1, col=1)

fig.add_trace(fig2['data'][0], row=1, col=2)
fig.add_trace(fig2['data'][1], row=1, col=2)
fig.add_trace(fig2['data'][2], row=1, col=2)

fig.add_trace(fig3['data'][0], row=2, col=1)
fig.add_trace(fig3['data'][1], row=2, col=1)
fig.add_trace(fig3['data'][2], row=2, col=1)

fig.add_trace(fig4['data'][0], row=2, col=2)
fig.add_trace(fig4['data'][1], row=2, col=2)
fig.add_trace(fig4['data'][2], row=2, col=2)

Colour schemes are too different and not representing similar values

The output


Solution

  • You can specify colours when you plot a bar chart.

    fig.add_trace(go.Bar( x = df.iloc[:,1], y = df.iloc[:,2], marker_color='black'), row=1, col=1)
    fig.add_trace(go.Bar( x = df.iloc[:,1], y = df.iloc[:,2], marker_color='red'), row=1, col=1)
    

    Alternatively, you can update traces in the end.

    color_list = [col1, col2, col3,...col9]
    for idx, d in enumerate(fig.data):
        d.marker.color = color_list[idx]