按总值对堆积柱形图进行排序 - Python

时间:2021-03-13 02:47:46

标签: python

我正在尝试根据总销量(y 轴)对下表进行排序 enter image description here

这是我绘制的数据框:

              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)')

1 个答案:

答案 0 :(得分:2)

如果您按照排序顺序对数据帧进行索引,则应该这样做:

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