I am trying to assign a custom colour to my Plotly Express Bar graph
I create the plot fine and it displays the data as I would like it. It just wonβt allow me to specify the colours for the bars.
If I try
fig.update_traces(marker_color=['#071633', '#0DEFFF'], showlegend=False)
I get the following plot which is the right colours just not on the correct bars
Can someone help me with assigning the colour #071633 to 2019 and #0DEFFF to 2020
My complete code for the chart creating and data is as follows.
# Our imaginary Data to be used to create our Pandas Series
dict_2019 = {'Legal' : 70,
'Police' : 90,
'Government' : 0,
'Army' : 70,
'Navy' : 58,
'Air Force' : 25,
'Coastguard' : 0,
'Ambulance' : 6,
'Counter-Terror' : 39,
'Direct Action' : 12
}
dict_2020 = {'Legal' : 95,
'Police' : 50,
'Government' : 24,
'Army' : 95,
'Navy' : 0,
'Air Force' : 65,
'Coastguard' : 0,
'Ambulance' : 0,
'Counter-Terror' : 49,
'Direct Action' : 25
}
# Create our Pandas Series
sf1 = pd.Series(dict_2019)
sf2 = pd.Series(dict_2020)
# Merge our Pandas Series
df_merge = pd.concat([sf1, sf2], axis = 1)
# Create our column names
df_merge.set_axis(column_names, axis = 'columns', inplace=True)
# Assign our new column names to the df_merge dataframe
df_merge.set_axis(column_names, axis = 'columns', inplace=True)
# Create our boolean mask
action_mask = (df_merge['2019'] > 0) & (df_merge['2020'] > 0)
# Draw our Plot
fig = px.bar(df_merge[action_mask])
# Change the bar mode
fig.update_layout(barmode='group',
title='<b>Number of actions by unit<b>',
xaxis_categoryorder = 'category ascending',
height = 800,
plot_bgcolor='rgba(0,0,0,0)',
showlegend=True
)
fig.update_traces(marker_color=['#071633', '#0DEFFF'], showlegend=True)
fig.update_xaxes(tickangle=45,
title_text='<b>Unit<b>',
showline=True,
linewidth=1,
linecolor='#071633'
)
fig.update_yaxes(title_text='<b>Count<b>',
showline=True,
linewidth=0.25,
linecolor='#071633')
fig.show()