【发布时间】:2020-04-02 02:16:13
【问题描述】:
这是我当前的代码,使用美国境内死因数据集(按发生次数):
`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)`
虽然这段代码没有给我任何错误,但它似乎只是以不同的随机顺序重新排列条形图,如下所示:
为什么会这样?我做错了什么,是否有某种方法可以将条形重新排列为我不知道的升序或降序?
【问题讨论】:
-
您获得的输出按
'cause_name'列排序,正如您在order中指定的那样。如果您想按其他内容进行排序,请这样做。 -
您按原因名称的字母顺序对条形图进行了排序。请改用
order=result['deaths']。 -
将 order=results 更改为使用 y 轴会完全删除所有条形,如下所示:gyazo.com/43de0615e82c55c367f93f5690e053dc