I have a data frame:
import pandas as pd
data = {
'BU': ['A', 'C', 'C', 'E', 'E', 'A', 'D', 'C', 'D', 'E', 'A', 'A', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'E', 'E', 'A', 'A'],
'Sub - BU': ['A2', 'B1', 'C1', 'D1', 'E1', 'A1', 'B1', 'C1', 'D1', 'E2', 'A1', 'B1', 'C1', 'D2', 'E2', 'A1', 'B1', 'C1', 'D1', 'E1', 'C1', 'D2', 'E1', 'E2', 'A1', 'A1'],
'Initial contract result': [0, 2, 0, 1, 3, 2, 0, 0, 0, 9, 0, 5, 2, 0, 0, 8, 7, 4, 0, 0, 0, 0, 5, 0, 8, 4]}
df_cur = pd.DataFrame(data)
df_cur['Contract Sum Type'] = df_cur['Initial contract result'].apply(lambda x: 'Zero' if x == 0 else 'Non-zero')
counts_df = df_cur.groupby(['BU', 'Sub - BU', 'Contract Sum Type']).size().reset_index(name='Count')
group_totals = counts_df.groupby(['BU', 'Sub - BU'])['Count'].transform('sum')
counts_df['Percentage'] = 100 * counts_df['Count'] / group_totals
counts_df['Total Count'] = counts_df.groupby('Sub - BU')['Count'].transform('sum')
Now I want to plot this as a stacked bar chart. On the x axis I want to see the Sub - BU but also I want it to be grouped by the BU column and each BU should get its own colors. Also the stacked bar chart should be descending. How to do this, because the fig.updatelayout commant doesnt seem to do anything in this also I tried creating a subset where I filter the dataframe descending based on the Total Count column but this didnt work.
import plotly.express as px
fig = px.bar(counts_df, x='Sub - BU', y='Count', color='Contract Sum Type',
title='Initial Contract Sum by Sub - BU', barmode='stack', text_auto=True)
fig.update_layout(yaxis={'categoryorder':'total ascending'})
fig.show()