2022-10-29

plotly: bar stacking graph

I have a Dataframe with data as follows:

data={'A': {'2020_01': 3, '2020_02': 3, '2020_03': 1, '2020_04': 3, '2020_05': 1},
 'B': {'2020_01': 0, '2020_02': 0, '2020_03': 3, '2020_04': 0, '2020_05': 2},
 'other': {'2020_01': 0,
  '2020_02': 0,
  '2020_03': 3,
  '2020_04': 0,
  '2020_05': 2},
 'total': {'2020_01': 3,
  '2020_02': 3,
  '2020_03': 7,
  '2020_04': 3,
  '2020_05': 5}}
df = pd.DataFrame(data)

I would like to represent with ploty in X the dates and in y stacked values of A, B, other

for one single bar I can do:

import plotly.express as px
fig = px.bar(df, x=df.index, y='A', text_auto=True,
             labels={'A':'bananas'}, height=400)
fig.show()

How to I have to proceed? I checked a bunch of documentation at no avail: https://community.plotly.com/t/plotly-express-bar-ignores-barmode-group/31511 https://plotly.com/python/bar-charts/ Plotly px.bar not stacking with barmode='stack' ...



No comments:

Post a Comment