有没有一种方法可以限制Python SEaborn条形图(或任何图表)的结果数量?

时间:2019-12-08 20:08:04

标签: python bar-chart seaborn

我正在尝试创建一个条形图,该条形图使用每年按发生率统计的死亡原因数据集。这是我当前的代码:

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 Five Leading Causes of Death in the United States',fontsize=20)

结果: https://refactoring.com/catalog/introduceParameterObject.html

有什么方法可以根据出现的总数来限制结果的数量,而不是图表显示的结果超过10个左右。在此特定示例中,我试图将其限制为前5个结果,但似乎无法使其正常工作。

1 个答案:

答案 0 :(得分:1)

基于my answer to your other question,您只需将order=参数中的原因包括在内即可轻松地将绘图限制在前5个死亡原因中:

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[1:6])

enter image description here