python - 使用 Python matplotlib 排序水平条
问题描述
我有以下代码使用 python 和 matplotlib 显示水平条形图。
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = (16.0, 10.0)
category_names = ['ProjectA', 'ProjectB', 'ProjectC', 'ProjectD', 'ProjectE']
results = {'Passed': [3, 4, 32, 6, 50],
'Failed': [2, 9, 60, 4, 68]}
df = pd.DataFrame(results, index=category_names)
ax = df.plot.barh(stacked=True, cmap='tab10', figsize=(16, 10))
for p in ax.patches:
left, bottom, width, height = p.get_bbox().bounds
if width > 0:
ax.annotate(f'{width:0.0f}', xy=(left+width/2, bottom+height/2), ha='center', va='center')
这将为我提供以下图表:
现在我的问题是,如何根据Passed
和Failed
列的总和对水平条进行降序排序?
解决方案
推荐阅读
- r - 在R中的字符串之前使用〜时是什么?
- javascript - 如何在按钮单击时关闭整个浏览器?
- powerbi - 在 CALCULATE 函数中,当提供超过 1 个过滤器表达式时,如何应用过滤器?
- git - 无法将对象迁移到永久存储
- python - 如何在列表中进行查询?
- reactjs - 反应原生 | 平面清单和备忘录 | renderItem 没有功能
- amazon-web-services - 无服务器框架的 Lambda 函数别名?
- node.js - 如何遍历多个用户以及 FireStore 中的一个用户
- android - Android 10 NFC 应用程序 - 在 MS Intune Kiosk 模式下手机睡眠时停止工作
- python - Python 多处理装饰器在酸洗时失败