Ordering a python Seaborn barplot by ascending or descending(按升序或降序排序一条蟒蛇海运条形图)
本文介绍了按升序或降序排序一条蟒蛇海运条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的当前代码,使用的是美国境内按发生次数排列的死因数据集:
`top_cause_of_death_barplot=sns.catplot(data=death, x='cause_name',
y='deaths',kind='bar',ci=None,legend_out=False,height=10, aspect=1.5)
plt.xlabel('Causes of Death',fontsize=15)
top_cause_of_death_barplot.set_xticklabels(fontsize=10)
plt.ylabel('Number of Observed Deaths',fontsize=15)
plt.title('Top Ten Leading Causes of Death in the United States (1999-2017)',fontsize=20)`
这将生成如下所示的图表:
我正在尝试重新排序图形,以使条形图按降序排列。我在代码中添加了一些内容,得到了如下结果:
`result = death.groupby(["cause_name"])
['deaths'].aggregate(np.median).reset_index().sort_values('cause_name')
top_cause_of_death_barplot=sns.catplot(data=death, x='cause_name',
y='deaths',kind='bar',ci=None,legend_out=False,height=10, aspect=1.5, order=result['cause_name'] )
plt.xlabel('Causes of Death',fontsize=15)
top_cause_of_death_barplot.set_xticklabels(fontsize=10)
plt.ylabel('Number of Observed Deaths',fontsize=15)
plt.title('Top Ten Leading Causes of Death in the United States (1999-2017)',fontsize=20)`
虽然此代码没有给我任何错误,但它似乎所做的只是以不同的、随机的顺序重新排序条形图,如下所示:
为什么会发生这种情况?我做错了什么,有什么方法可以将栏重新排列为我不知道的升序或降序吗?
推荐答案
您必须将x=
的值传递到order=
。在你的情况下,我会这样做:
death = pd.read_csv('https://storage.googleapis.com/hewwo/NCHS_-_Leading_Causes_of_Death__United_States.csv', sep=',', header=0)
plot_order = death.groupby('Cause Name')['Deaths'].sum().sort_values(ascending=False).index.values
sns.catplot(data=death, x='Cause Name', y='Deaths',kind='bar',ci=None, legend_out=False, order=plot_order)
或者,如果您想要删除"所有原因"栏:
sns.catplot(data=death, x='Cause Name', y='Deaths',kind='bar',ci=None, legend_out=False, order=plot_order[1:])
这篇关于按升序或降序排序一条蟒蛇海运条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:按升序或降序排序一条蟒蛇海运条形图


基础教程推荐
猜你喜欢
- 求两个直方图的卷积 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01