【问题标题】:Is there a way to limit the number of results for a Python SEaborn bar-plot (or any chart)?有没有办法限制 Python SEaborn 条形图(或任何图表)的结果数量?
【发布时间】:2019-12-08 20:08:04
【问题描述】:

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

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)

结果: SEaborn bar-plot from code

有没有办法根据出现的总数来限制结果的数量,而不是图表显示超过 10 个左右的结果。在这个具体的例子中,我试图将它限制在前 5 个结果中,但似乎无法让它发挥作用。

【问题讨论】:

  • 这不是绘图问题。您需要创建一个仅包含您要显示的条目的数据框。

标签: python bar-chart seaborn


【解决方案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])

【讨论】:

  • 这正是我所缺少的。我知道 order 参数是必需的,但“1:6”是迄今为止它实际做了一些事情的唯一方法。谢谢。
猜你喜欢
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 2020-02-28
  • 2021-10-30
  • 2020-10-09
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多