Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

python - Adding Statistical Significance Annotations to Barplot Subplots

I've been trying to figure out how to add statistical significance bars to specific subplots. Most related questions have been specific to a single plot, but does not show how to arbitrarily add statistical annotations to a subplot. Can someone show me how to add similar statistical annotations that are shown in the 4th subplot to each of the other subplots?

Here's the code and output that I get:

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

question from:https://stackoverflow.com/questions/65557702/adding-statistical-significance-annotations-to-barplot-subplots

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to write a function that takes an Axes as an argument and use the OO-version of the functions to draw on that Axes object:

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)

example:

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()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...