使用seaborn的绘图系列 [英] Plotting series using seaborn
问题描述
category = df.category_name_column.value_counts()
我具有上面的返回值的系列:
I have the above series which returns the values:
CategoryA,100
CategoryB,200
我试图在X轴上绘制前5个类别名称,在y轴上绘制值
I am trying to plot the top 5 category names in X - axis and values in y-axis
head = (category.head(5))
sns.barplot(x = head ,y=df.category_name_column.value_counts(), data=df)
它不会在X轴上打印类别的名称",而是显示计数.如何在X中打印前5名,在Y中打印值?
It does not print the "names" of the categories in the X-axis, but the count. How to print the top 5 names in X and Values in Y?
推荐答案
您可以传入系列的 index
& values
到 x
& y
分别在 sns.barplot
中.这样,绘图代码将变为:
You can pass in the series' index
& values
to x
& y
respectively in sns.barplot
. With that the plotting code becomes:
sns.barplot(head.index, head.values)
我正在尝试绘制X中排名前5位的类别名称
I am trying to plot the top 5 category names in X
调用 category.head(5)
将返回系列 category
中的前五个值,该值可能与 top 5 不同基于每个类别出现的次数.如果您要选择5个最常见的类别,则必须先对系列进行排序&;然后调用 head(5)
.像这样:
calling category.head(5)
will return the first five values from the series category
, which may be different than the top 5 based on the number of times each category appears. If you want the 5 most frequent categories, it is necessary to sort the series first & then call head(5)
. Like this:
category = df.category_name_column.value_counts()
head = category.sort_values(ascending=False).head(5)
这篇关于使用seaborn的绘图系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!