简体   繁体   English

使用数据框在 Seaborn 中的水平条形图

[英]Horizontal barplot in Seaborn using dataframe

I am struggling with barplots in seaborn and I am not sure what I am doing wrong.我在 seaborn 中挣扎于条形图,我不确定我做错了什么。 The data is very simple:数据很简单:

name     totalCount
Name1    2000
Name2    40000
Name3    50000

sns.barplot(x='name',y='totalCount',data=df)

produces a bar plot that has mean(totalCount) instead of the actual count.生成一个具有 mean(totalCount) 而不是实际计数的条形图。

sns.countplot('name',data=df)

produces a bar plot with all count values on y-axis equal to 1.生成一个条形图,y 轴上的所有计数值都等于 1。

How do I generate the plot that has:如何生成具有以下特征的图:

totalCount on x-axis, name on y-axis? x 轴上的 totalCount,y 轴上的名称?

Usually when plotting a bar chart, a vector of values is passed in per category level, and some function is applied to evaluate each vector to determine the length of each bar.通常在绘制条形图时,每个类别级别都会传递一个值向量,并应用一些函数来评估每个向量以确定每个条形的长度。

In your case, you already have a count and just want a bar that's as long as the count value.在您的情况下,您已经有了一个计数,并且只想要一个与计数值一样长的条形。

It doesn't really matter that Seaborn's default estimator is mean , it should still give you what you're looking for, as it's just taking the mean of a single value, for each category level. Seaborn 的默认估算器是mean并不重要,它仍然应该为您提供您正在寻找的东西,因为它只是针对每个类别级别取单个值的平均值。 The estimation function can be changed by invoking the estimator argument, eg estimator=sum , but in this case you don't need to.可以通过调用estimator参数来更改estimator函数,例如estimator=sum ,但在这种情况下您不需要。 You just need to change the axis label:您只需要更改轴标签:

ax = sns.barplot(x='totalCount', y='name', data=df)
ax.set_xlabel('totalCount')

单杠

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM