使用seaborn绘制系列

Hac*_*rds 2 python data-visualization matplotlib pandas seaborn

category = df.category_name_column.value_counts()  
Run Code Online (Sandbox Code Playgroud)

我有以上系列返回值:

CategoryA,100
CategoryB,200
Run Code Online (Sandbox Code Playgroud)

我试图在X轴上绘制前5个类别名称,在y轴上绘制值

head = (category.head(5)) 
sns.barplot(x = head ,y=df.category_name_column.value_counts(), data=df)
Run Code Online (Sandbox Code Playgroud)

它不会在X轴上打印类别的"名称",而是打印计数.如何打印X中的前5个名称和Y中的值?

Hal*_*Ali 12

您可以在一系列的传递indexvaluesx&y分别sns.barplot.有了它,绘图代码变为:

sns.barplot(head.index, head.values)
Run Code Online (Sandbox Code Playgroud)

我试图在X中绘制前5个类别名称

呼叫category.head(5)将从系列返回第五个值category,其可以是比不同顶部5基于次出现的每个类别的数目.如果您想要5个最常见的类别,则需要先对系列进行排序,然后再进行调用head(5).像这样:

category = df.category_name_column.value_counts()
head = category.sort_values(ascending=False).head(5)
Run Code Online (Sandbox Code Playgroud)