I am using this code to generate a bar plot.
import pandas as pd
import plotly.express as px
df = pd.read_csv(...)
# Melt the DataFrame to long format
melted_df = df.melt(id_vars=["Environment", "Dataset"], value_vars=["Alpha0.5Diff", "Alpha1Diff", "Alpha2Diff"],
var_name="α")
melted_df['α'] = melted_df['α'].replace('Alpha0.5Diff', "0.5")
melted_df['α'] = melted_df['α'].replace('Alpha1Diff', "1")
melted_df['α'] = melted_df['α'].replace('Alpha2Diff', "2")
# Create a bar chart with subplots for each "Alpha" value
fig = px.bar(melted_df, x="Environment", y="value", color="Dataset", facet_col="α",
category_orders={"α": ["0.5", "1", "2"]},
labels={"Environment": "Environment", "value": "Percent Difference"})
fig.update_layout(
yaxis=dict(title_font=dict(size=24), tickfont=dict(size=24)), # Increase y-axis label font size
font=dict(size=20), # Increase text font size for other elements
# facet_col_title_font=dict(size=24), # Increase facet (subplot) title font size
legend=dict(font=dict(size=30)),
barmode='overlay',
legend_title_text="Dataset"
)
# Set opacity for the bars to distinguish overlapping bars
fig.update_traces(marker=dict(opacity=0.4), selector=dict(type='bar'))
# update the
for i in range(len(fig.data)):
fig.update_xaxes(title_text="Environment", row=1, col=i+1, title_font=dict(size=24))
fig.update_annotations(font_size=36) # control subplot function size
# Move the legend to the right-hand subplot at the bottom
fig.update_layout(legend=dict(x=0.89, y=0.00005))
# Show the plot
fig.write_image("...", width=2500, height=1500)
However, I would like to modify the colours used for the bars. I tried setting up a dummy column in the dataset which assigned a colour based on the dataset; however, this changes the legend to be ‘Colour: red blue green’. Is there a way to either choose colours themselves or to use the dummy column and alter the legend?