[英]Plotting series using seaborn
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.它不会在 X 轴上打印类别的“名称”,而是打印计数。 How to print the top 5 names in X and Values in Y?
如何打印 X 中的前 5 个名称和 Y 中的值?
You can pass in the series' index
& values
to x
& y
respectively in sns.barplot
.您可以在
sns.barplot
中分别将系列的index
和values
传递给x
和y
。 With that the plotting code becomes:这样,绘图代码变为:
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 5 based on the number of times each category appears.调用
category.head(5)
将返回系列category
的前五个值,根据每个类别出现的次数,这可能与前 5 个不同。 If you want the 5 most frequent categories, it is necessary to sort the series first & then call head(5)
.如果您想要 5 个最常见的类别,则必须先对系列进行排序,然后调用
head(5)
。 Like this:像这样:
category = df.category_name_column.value_counts()
head = category.sort_values(ascending=False).head(5)
Since the previous accepted solution is deprecated in seaborn .由于以前接受的解决方案在 seaborn 中已被弃用。 Another workaround could be as follows:
另一种解决方法可能如下:
category = df.category_name_column.value_counts()
category_df = category.reset_index()
category_df.columns = ['categories', 'frequency']
ax = sns.barplot(x = 'categories', y = 'frequency', data = category_df)
Although this is not exactly plot of series, this is a workaround that's officially supported by seaborn.虽然这不完全是系列情节,但这是 seaborn 官方支持的解决方法。
For more barplot examples please refer here:有关更多条形图示例,请参阅此处:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.