I have a dataframe which results from:
df_grouped = df.groupby(['A', 'B'])['A'].count().sort_values(ascending=False)
df_grouped = pd.DataFrame(df_grouped)
new_index = pd.MultiIndex.from_tuples(df_grouped.index)
df_grouped.index = new_index
df_grouped.reset_index(inplace=True)
df_grouped.columns = ['A', 'B', 'count']
Then, df_grouped is something like:
| A | B | count |
|---|---|---|
| A_1 | B_1 | 10 |
| A_1 | B_2 | 51 |
| A_1 | B_3 | 25 |
| A_1 | B_4 | 12 |
| A_1 | B_5 | 2 |
| A_2 | B_1 | 19 |
| A_2 | B_3 | 5 |
| A_3 | B_5 | 18 |
| A_3 | B_4 | 33 |
| A_3 | B_5 | 44 |
| A_4 | B_1 | 29 |
| A_5 | B_2 | 32 |
I have plotted a seaborn.histplot using the following code:
fig, ax = plt.subplots(1, 1, figsize=(10,5))
sns.histplot(x='A', hue='B', data=df_grouped, ax=ax, multiple='stack', weights='count')
and results in the following image:

What I would like is to order the plot based on the total counts of each value of A. I have tried different methods, but I am not able to get a successful result.
Edit
I found a way to do what I wanted.
What I did, is to calculate the total counts by df['A'] values:
df['total_count'] = df.groupby(by='A')['count'].transform('sum')
df = df.sort_values(by=['total_count'], ascending=False)
Then, by using the same plot code from above, I got the desired result.
The answer is similar to what Redox proposed.
In any case, I will try the other options proposed.

