pandas 使用 seaborn 绘制系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46793448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:39:14  来源:igfitidea点击:

Plotting series using seaborn

pythonpandasmatplotlibdata-visualizationseaborn

提问by Hackerds

category = df.category_name_column.value_counts()  

I have the above series which returns the values:

我有上面的系列返回值:

CategoryA,100
CategoryB,200

I am trying to plot the top 5 category names in X - axis and values in y-axis

我正在尝试绘制 X 轴中的前 5 个类别名称和 y 轴中的值

head = (category.head(5)) 
sns.barplot(x = head ,y=df.category_name_column.value_counts(), data=df)

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?

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

回答by Haleemur Ali

You can pass in the series' index& valuesto x& yrespectively in sns.barplot. With that the plotting code becomes:

您可以在系列的传递indexvaluesxy分别sns.barplot。这样绘图代码就变成了:

sns.barplot(head.index, head.values)

I am trying to plot the top 5 category names in X

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

calling category.head(5)will return the first five values from the series category, which may be different than the top 5based 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.head(5)将返回 series 的前五个值,根据每个类别出现的次数category,它可能与前 5 个不同。如果你想要 5 个最频繁的类别,需要先对系列进行排序,然后调用head(5). 像这样:

category = df.category_name_column.value_counts()
head = category.sort_values(ascending=False).head(5)