代码之家  ›  专栏  ›  技术社区  ›  Hackerds

使用seaborn绘制序列

  •  6
  • Hackerds  · 技术社区  · 8 年前
    category = df.category_name_column.value_counts()  
    

    CategoryA,100
    CategoryB,200
    

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

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

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

    2 回复  |  直到 6 年前
        1
  •  25
  •   Haleemur Ali    8 年前

    你可以通过这个系列' index & values x & y 分别位于 sns.barplot . 至此,绘图代码变为:

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

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

    使命感 category.head(5) 将返回序列中的前五个值 category ,这可能不同于 根据每个类别出现的次数。如果您想要5个最常见的类别,有必要首先对序列进行排序;然后打电话 head(5) . 这样地:

    category = df.category_name_column.value_counts()
    head = category.sort_values(ascending=False).head(5)
    
        2
  •  0
  •   Dan    4 年前

    deprecated in seaborn . 另一种解决方法如下:

    1. 将序列转换为数据帧
    category = df.category_name_column.value_counts()  
    category_df = category.reset_index()
    category_df.columns = ['categories', 'frequency']
    
    1. 使用条形图
    ax = sns.barplot(x = 'categories', y = 'frequency', data = category_df)
    

    虽然这不完全是系列的情节,但这是一个由seaborn官方支持的解决方案。

    有关更多条形图示例,请参阅此处:

    1. https://seaborn.pydata.org/generated/seaborn.barplot.html
    2. https://stackabuse.com/seaborn-bar-plot-tutorial-and-examples/