首页 > 解决方案 > 按总值对堆积柱形图进行排序 - Python

问题描述

我正在尝试根据售出的总单位数(y 轴)对下表进行排序 在此处输入图像描述

这是我绘制的数据框:

              JP_Sales  NA_Sales  EU_Sales
Genre                                     
Action          158.66    861.80    516.48
Adventure        52.01    102.06     63.79
Fighting         87.15    220.74    100.00
Misc            106.67    402.48    213.82
Platform        130.65    445.99    200.67
Puzzle           56.68    122.01     50.53
Racing           56.61    356.93    236.32
Role-Playing    350.29    326.50    187.58
Shooter          38.18    575.16    310.45
Simulation       63.54    181.78    113.20
Sports          134.76    670.09    371.34
Strategy         49.10     67.89     44.94

使用此代码:

Genre_sales.plot.bar(stacked=True, figsize = (10,10))
plt.suptitle('Total Sales by Genre, separated by Region')
plt.xlabel('Genre')
plt.ylabel('Total Units Sold (Millions)')

标签: python

解决方案


如果您使用排序顺序索引到数据框,那么应该这样做:

Genre_sales.loc[df.sum(axis=1).sort_values().index].plot.bar(stacked=True, figsize=(10,10))

推荐阅读