I have some dataframes I want to plot with a variable number of Fruits. When plotting, each dataframe will always have the same Fruit types, even though some may have a value of 0 in their respective row.
df1
Day Value Fruit
1 5 Banana
1 3 Pear
2 4 Banana
2 2 Pear
3 3 Banana
3 3 Pear
df2 # note all pears 0'd
Day Value Fruit
1 5 Banana
1 0 Pear
2 4 Banana
2 0 Pear
3 3 Banana
3 0 Pear
# AKA
df1.Fruit.unique() == df2.Fruit.unique()
Plotting code:
import dash_bootstrap_components as dbc
from plotly import express as px
bar_charts = []
for df in dfs:
# to get a stacked bar chart
fruit_data = df.groupby(["Day", "Fruit"]).Value.sum().to_frame().reset_index()
bar_charts.append(
dcc.Graph(
figure=px.bar(
fruit_data,
x="Day",
y="Value",
color="Fruit",
),
)
)
return dbc.Col(bar_charts)
Plotly seems to be assigning differnt colors to the Fruit between dataframes based on the value of the Fruit. I want the Fruits to always be the same color across graphs regardless of value (e.g. Banana always yellow, Pear always green). HOWEVER, I don't know what Fruits are available until runtime, so I can't just hard code a color map ahead of time.
How do I tell plotly to always color the Fruit the same?
Im sure this is some silly little option im missing.