【问题标题】:Can I use seaborn.countplot to display my data?我可以使用 seaborn.countplot 来显示我的数据吗?
【发布时间】:2020-11-01 23:06:36
【问题描述】:

我在使用 seaborn 显示一些 HR 分数数据时遇到问题。 我想使用计数图来可视化 y 轴上的员工数据 以色调为评分的类别,以x轴为评分的取值范围。

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

btraits = {'Behavioural Traits': ['Communicaiton', 'Teamwork', 'Leadership', 'Negotiation', 'Agreeableness'],
'James':[0,-2,0,-2,-2],
'John':[2,0,0,2,-1],
'Gary':[0,-1,0,1,-3],
'Raymond':[3,-5,0,1,0]}
df = pd.DataFrame(btraits)
df.set_index('Behavioural Traits', inplace=True)

sns.countplot(data=btraits, y='columns', hue='index')

【问题讨论】:

    标签: python pandas matplotlib seaborn bar-chart


    【解决方案1】:
    • 我认为您需要 barplot 来获取此数据。
    • countplot 使用条形图显示每个分类箱中的观察计数。
    • 数据需stacked转成长格式
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    btraits = {'Behavioural Traits': ['Communicaiton', 'Teamwork', 'Leadership', 'Negotiation', 'Agreeableness'],
               'James': [0,-2,0,-2,-2],
               'John': [2,0,0,2,-1],
               'Gary': [0,-1,0,1,-3],
               'Raymond': [3,-5,0,1,0]}
    
    df = pd.DataFrame(btraits)
    df.set_index('Behavioural Traits', inplace=True)
    
                        James  John  Gary  Raymond
    Behavioural Traits                            
    Communicaiton           0     2     0        3
    Teamwork               -2     0    -1       -5
    Leadership              0     0     0        0
    Negotiation            -2     2     1        1
    Agreeableness          -2    -1    -3        0
    
    # stack the columns
    dfs = df.stack().reset_index().rename(columns={'level_1': 'names', 0: 'values'})
    
       Behavioural Traits    names  values
    0       Communicaiton    James       0
    1       Communicaiton     John       2
    2       Communicaiton     Gary       0
    3       Communicaiton  Raymond       3
    4            Teamwork    James      -2
    5            Teamwork     John       0
    6            Teamwork     Gary      -1
    7            Teamwork  Raymond      -5
    8          Leadership    James       0
    9          Leadership     John       0
    10         Leadership     Gary       0
    11         Leadership  Raymond       0
    12        Negotiation    James      -2
    13        Negotiation     John       2
    14        Negotiation     Gary       1
    15        Negotiation  Raymond       1
    16      Agreeableness    James      -2
    17      Agreeableness     John      -1
    18      Agreeableness     Gary      -3
    19      Agreeableness  Raymond       0
    

    计数图

    sns.countplot(data=dfs, x='names', hue='Behavioural Traits')
    

    条形图

    sns.barplot(x='names', y='values', hue='Behavioural Traits', data=dfs)
    

    【讨论】:

      【解决方案2】:

      Countplot 基本上返回出现次数。

      您可以尝试 barplot 来代替每个分类变量。 例如:sns.barplot(x=df.index,y='James',data=df)

      【讨论】:

        猜你喜欢
        • 2015-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多