【问题标题】:Adding Statistical Significance Annotations to Barplot Subplots向条形图子图添加统计显着性注释
【发布时间】:2021-04-09 22:51:28
【问题描述】:

我一直在试图弄清楚如何将统计显着性条添加到特定的子图中。大多数相关问题都特定于单个图,但没有显示如何任意向子图添加统计注释。有人可以告诉我如何将第 4 个子图中显示的类似统计注释添加到其他每个子图中吗?

这是我得到的代码和输出:

zero.columns= ['Number of Research Years', 'Total Publications', 'Publications During Residency',
               'Publications Before & After Residency', 'H Index']

fig, axes = plt.subplots(2, 2, sharex= False, sharey= True, figsize=(8,8))

ax1= sns.barplot(ax=axes[0,0], x= zero['Number of Research Years'], y= zero['Total Publications'])
ax2= sns.barplot(ax=axes[0,1], x= zero['Number of Research Years'], y= zero['Publications During Residency'])
ax3= sns.barplot(ax=axes[1,0], x= zero['Number of Research Years'], y= zero['Publications Before & After Residency'])
ax4= sns.barplot(ax=axes[1,1], x= zero['Number of Research Years'], y= zero['H Index'])

x1, x2 = 1, 2   
y, h, col = 100, 5, 'k'
plt.plot([x1, x1, x2, x2], [y, y+h, y+h, y], lw=1.5, c=col)
plt.text((x1+x2)*.5, y+h, "*", ha='center', va='bottom', color=col)

x1, x2 = 0, 2  
y, h, col = 120, 5, 'k'
plt.plot([x1, x1, x2, x2], [y, y+h, y+h, y], lw=1.5, c=col)
plt.text((x1+x2)*.5, y+h, "*", ha='center', va='bottom', color=col)

ax1.text(0.05, 0.95, "A", fontweight="bold", transform=ax1.transAxes)
ax2.text(0.05, 0.95, "B", fontweight="bold", transform=ax2.transAxes)
ax3.text(0.05, 0.95, "C", fontweight="bold", transform=ax3.transAxes)
ax4.text(0.05, 0.95, "D", fontweight="bold", transform=ax4.transAxes)


sns.despine()
plt.show()

The subplots that I have created so far

【问题讨论】:

    标签: python python-3.x seaborn subplot significance


    【解决方案1】:

    您需要编写一个以Axes 作为参数的函数,并使用the OO-version of the functions 在该Axes 对象上绘图:

    def annot_stat(star, x1, x2, y, h, col='k', ax=None):
        ax = plt.gca() if ax is None else ax
        ax.plot([x1, x1, x2, x2], [y, y+h, y+h, y], lw=1.5, c=col)
        ax.text((x1+x2)*.5, y+h, star, ha='center', va='bottom', color=col)
    

    示例:

    def annot_stat(star, x1, x2, y, h, col='k', ax=None):
        ax = plt.gca() if ax is None else ax
        ax.plot([x1, x1, x2, x2], [y, y+h, y+h, y], lw=1.5, c=col)
        ax.text((x1+x2)*.5, y+h, star, ha='center', va='bottom', color=col)
        
    zero = pd.DataFrame({'Number of Research Years': [0,1,2], 
                         'Total Publications': 100*np.random.random(size=3),
                         'Publications During Residency': 100*np.random.random(size=3),
                         'Publications Before & After Residency': 100*np.random.random(size=3), 
                         'H Index': 10*np.random.random(size=3)})
    
    fig, axes = plt.subplots(2, 2, sharex= False, sharey= True, figsize=(8,8))
    
    ax1= sns.barplot(ax=axes[0,0], x= zero['Number of Research Years'], y= zero['Total Publications'])
    ax2= sns.barplot(ax=axes[0,1], x= zero['Number of Research Years'], y= zero['Publications During Residency'])
    ax3= sns.barplot(ax=axes[1,0], x= zero['Number of Research Years'], y= zero['Publications Before & After Residency'])
    ax4= sns.barplot(ax=axes[1,1], x= zero['Number of Research Years'], y= zero['H Index'])
    
    ax1.text(0.05, 0.95, "A", fontweight="bold", transform=ax1.transAxes)
    ax2.text(0.05, 0.95, "B", fontweight="bold", transform=ax2.transAxes)
    ax3.text(0.05, 0.95, "C", fontweight="bold", transform=ax3.transAxes)
    ax4.text(0.05, 0.95, "D", fontweight="bold", transform=ax4.transAxes)
    
    annot_stat('*', 1, 2, 100, 5, ax=ax1)
    annot_stat('***', 0, 2, 120, 5, ax=ax1)
    
    annot_stat('*', 1, 2, 100, 5, ax=ax2)
    annot_stat('***', 0, 2, 120, 5, ax=ax2)
    
    annot_stat('*', 1, 2, 100, 5, ax=ax3)
    annot_stat('***', 0, 2, 120, 5, ax=ax3)
    
    annot_stat('***', 1, 2, 10, 5, ax=ax4)
    annot_stat('*', 0, 2, 20, 5, ax=ax4)
    
    
    sns.despine()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2017-10-30
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      相关资源
      最近更新 更多