I'm trying to create a bar chart using plotly in python, which is both stacked and grouped.
Toy example (money spent and earned in different years):
import pandas as pd
import plotly.graph_objs as go
data = pd.DataFrame(
dict(
year=[2000,2010,2020],
var1=[10,20,15],
var2=[12,8,18],
var3=[10,17,13],
var4=[12,11,20],
)
)
fig = go.Figure(
data = [
go.Bar(x=data['year'], y=data['var1'], offsetgroup=0, name='spent on fruit'),
go.Bar(x=data['year'], y=data['var2'], offsetgroup=0, base=data['var1'], name='spent on toys'),
go.Bar(x=data['year'], y=data['var3'], offsetgroup=1, name='earned from stocks'),
go.Bar(x=data['year'], y=data['var4'], offsetgroup=1, base=data['var3'], name='earned from gambling'),
]
)
fig.show()
The result seems fine at first:
But watch what happens when I turn off e.g. "spent on fruit":
The "spent on toys" trace remains floating instead of starting from 0.
Can this be fixed? or maybe the whole offsetgroup
+ base
approach won't work here. But what else can I do?
Thanks!
Update: according to this Github issue, stacked, grouped bar plots are being developed for future plotly versions, so this probably won't be an issue anymore.