【问题标题】:Ordering a python Seaborn barplot by ascending or descending通过升序或降序订购 python Seaborn barplot
【发布时间】: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

标签: python pandas seaborn


【解决方案1】:

您可以将sns.catplot()order 参数设置为您喜欢的顺序。您可以使用df['col'].value_counts().index 获取此订单。您尚未提供数据示例,因此请允许我提供一个易于重现的示例。

import seaborn as sns
import pandas as pd
import numpy as np

a = np.random.choice(['cat', 'dog', 'hamster'], 100)

df = pd.DataFrame(a, columns=['animals'])

sns.catplot(data=df, x='animals',kind='count',ci=None,legend_out=False,
            height=3, aspect=1.5, order=df['animals'].value_counts().index)

【讨论】:

  • 抱歉,忘记包含我正在使用的数据集。如果您仍想查看,请点击此处:storage.googleapis.com/hewwo/…。你用的方法不是和我一样吗?我使用 order 语句,所做的只是按字母顺序而不是按数字排序。
【解决方案2】:

您必须将 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:])

【讨论】:

    猜你喜欢
    • 2018-08-01
    • 2021-12-31
    • 1970-01-01
    • 2021-07-06
    • 2016-07-01
    • 1970-01-01
    • 2017-11-11
    • 2013-05-13
    相关资源
    最近更新 更多