使用数据框在 Seaborn 中的水平条形图 [英] Horizontal barplot in Seaborn using dataframe
问题描述
我在 seaborn 中挣扎于条形图,我不确定我做错了什么.数据很简单:
I am struggling with barplots in seaborn and I am not sure what I am doing wrong. The data is very simple:
name totalCount
Name1 2000
Name2 40000
Name3 50000
sns.barplot(x='name',y='totalCount',data=df)
生成一个柱状图,其中包含 mean(totalCount) 而不是实际计数.
produces a bar plot that has mean(totalCount) instead of the actual count.
sns.countplot('name',data=df)
生成一个条形图,y 轴上的所有计数值都等于 1.
produces a bar plot with all count values on y-axis equal to 1.
如何生成具有以下内容的图:
How do I generate the plot that has:
x 轴为 totalCount,y 轴为名称?
totalCount on x-axis, name on y-axis?
推荐答案
通常在绘制条形图时,每个类别级别都会传入一个值向量,并应用一些函数来评估每个向量以确定每个向量的长度酒吧.
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.
Seaborn 的默认估算器是 mean
并不重要,它仍然应该为您提供您正在寻找的东西,因为它只是针对每个类别级别取单个值的平均值.可以通过调用 estimator
参数来更改估计函数,例如estimator=sum
,但在这种情况下您不需要.您只需要更改轴标签:
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. The estimation function can be changed by invoking the estimator
argument, e.g. estimator=sum
, but in this case you don't need to. You just need to change the axis label:
ax = sns.barplot(x='totalCount', y='name', data=df)
ax.set_xlabel('totalCount')
这篇关于使用数据框在 Seaborn 中的水平条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!