all,
I have a dataframe which I have grouped and sorted and that looks like this
brkrcy=data[data['upload_date']==Date].groupby(['CPTY','currency'], as_index=False).agg({"Gross Loan Amount": "sum"})
brkrcy=brkrcy.sort_values(by=['CPTY', 'Gross Loan Amount'], ascending=[True, False])
brkrcy = brkrcy.set_index('CPTY')
Double Ranking
currency Gross Loan Amount
CPTY
BARC RUB 2.178780e+07
BARC ZAR 7.779714e+07
BARC JPY 1.227676e+09
BARC EUR 3.301354e+09
BARC GBP 5.002534e+09
BARC USD 6.667446e+09
BMON CAD 2.018614e+08
BMON GBP 4.096820e+08
BMON USD 6.510318e+08
BNP CAD 2.349053e+08
BNP JPY 1.523716e+09
BNP GBP 3.234833e+09
BNP USD 4.576760e+09
BNP EUR 4.935927e+09
CALIP EUR 1.832390e+07
CALIP USD 1.448161e+09
CALIP GBP 3.492144e+09
CANTR USD 3.987880e+08
CIBC CAD 6.851792e+08
CIBC GBP 8.861776e+08
CITI CZK 7.549203e+06
brkrcy.set_index('currency',append=True)['Gross Loan Amount'].unstack().plot(kind="bar",stacked=True,figsize=(10,8))
plt.ylabel('Gross Loan Amount in Billions')
plt.show()
As you can see although it is double ranking, the stacked bar plot is not ranked in descending order. How can I change that please?
Assuming you mean descending bars on your stacked plot, consider adding a helper Total column that sums all currency fields of your plot dataframe by each CPTY. Use this new column to sort the data in descending order, then drop the helper column before plotting:
To demonstrate with random data that hopefully replicates your actual data (seeded for reproducibility):
Data
Plot