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

用另一列中的值注释seaborn barplot

  •  0
  • oymonk  · 技术社区  · 11 月前

    我创建了一个分组条形图,表示每个时间间隔赢得的案件百分比。我想用每个时间间隔赢得的案例数来注释条形图。

    这是我的代码:

    import pandas as pd
    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    
    df = pd.DataFrame({
        'years': ['1994-1998','1999-2003','2004-2008','2009-2013','2013-2017','2018-2022'],
        'Starfish number of cases': [10,8,31,12,2,3],
        'Starfish percent of wins': [0,0.25,0.225806451612903,0.416666666666666,1,0],
        'Jellyfish number of cases':[597,429,183,238,510,595],
        'Jellyfish percent of wins':[0.362646566164154,0.273892773892773,0.423497267759562,0.478991596638655,0.405882352941176,0.408403361344537],
    
    })
    
    df = pd.melt(df, id_vars=['years'], value_vars=['Starfish percent of wins', 'Jellyfish percent of wins'])
    
    sns.set_theme(style="whitegrid")
    
    
    # Initialize the matplotlib figure
    f, ax = plt.subplots(figsize=(30, 15))
    
    sns.barplot(x="years", y="value", hue='variable', data=df)
    
    
    for p in ax.patches:
        ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))
    

    我已经尝试将案例数量包括在熔融函数中 (即。 df = pd.melt(df, id_vars=['years'], value_vars=['Starfish number of cases','Jellyfish number of cases','Starfish percent of wins', 'Jellyfish percent of wins']) )但是这增加了表示病例总数的附加条。

    我试图修改答案 here 通过添加以下行,但结果显示的是百分比注释,而不是案例数:

    for p,years in zip(ax.patches, df['Starfish number of cases','Jellyfish number of cases']):
        ax.annotate(years, xy=(p.get_x()+p.get_width()/2, p.get_height()),
                    ha='center', va='bottom')
    

    有一个答案 here ,但这很复杂。一定有更简单的方法吗?

    1 回复  |  直到 11 月前
        1
  •  1
  •   JohanC    11 月前

    以下方法添加了要包含在熔体中的“案例数”列。然后,只使用百分比创建条形图。

    钢筋储存在 ax.containers 有两个容器,每个色调值一个容器。 ax.bar_label() 可以获得一个容器和一个标签列表作为输入。

    import matplotlib.pyplot as plt
    import seaborn as sns
    import pandas as pd
    import numpy as np
    
    df_orig = pd.DataFrame({
        'years': ['1994-1998', '1999-2003', '2004-2008', '2009-2013', '2013-2017', '2018-2022'],
        'Starfish number of cases': [10, 8, 31, 12, 2, 3],
        'Starfish percent of wins': [0, 0.25, 0.2258064516, 0.41666666666, 1, 0],
        'Jellyfish number of cases': [597, 429, 183, 238, 510, 595],
        'Jellyfish percent of wins': [0.3626465661, 0.2738927739, 0.4234972677, 0.4789915966, 0.4058823529, 0.4084033613],
    })
    
    df = pd.melt(df_orig, id_vars=['years'],
                 value_vars=['Starfish number of cases', 'Starfish percent of wins',
                             'Jellyfish number of cases', 'Jellyfish percent of wins'])
    
    sns.set_theme(style="whitegrid")
    
    # Initialize the matplotlib figure
    fig, ax = plt.subplots(figsize=(12, 5))
    
    sns.barplot(x="years", y="value", hue='variable',
                hue_order=['Starfish percent of wins', 'Jellyfish percent of wins'], data=df, ax=ax)
    
    for bargroup, variable in zip(ax.containers, ['Starfish number of cases', 'Jellyfish number of cases']):
        labels = ['' if val == 0.0 else f'{val:.0f}' for val in df[df['variable'] == variable]['value']]
        ax.bar_label(bargroup, labels)
    sns.despine()
    

    seaborn barplot with labels from other column