简体   繁体   English

使用Seaborn barplot绘制大熊猫分类系列

[英]Plot a pandas categorical Series with Seaborn barplot

I would like to plot the result of the values_counts() method with seaborn , but when I do so, it only shows one of the variables. 我想用seaborn绘制values_counts()方法的seaborn ,但是当我这样做时,它仅显示变量之一。

df = pd.DataFrame({"A":['b','b','a','c','c','c'],"B":['a','a','a','c','b','d']})
counts = df.A.value_counts()
sns.barplot(counts)

以上代码的结果

I want a barplot showing heights of 'a' = 1, 'b' = 2, 'c' = 3 我想要一个显示高度为'a' = 1, 'b' = 2, 'c' = 3的条形图

I've tried renaming the index and passing in x and y parameters, but I cant' get it to work. 我尝试重命名索引并传递xy参数,但无法使其正常工作。

You can do this: 你可以这样做:

# Sorting indices so it's easier to read 
counts.sort_index(inplace=True)

sns.barplot(x = counts.index, y = counts)
plt.ylabel('counts')

在此处输入图片说明

Note that using pandas.Series.plot gives a very similar plot: counts.plot('bar') or counts.plot.bar() 请注意,使用pandas.Series.plot会给出非常相似的图: counts.plot('bar')counts.plot.bar()

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

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