繁体   English   中英

如何使用Python的Matplotlib在条形图上添加显着性水平?

[英]How to add significance levels on bar graph using Python's Matplotlib?

我已经编写了一些代码来绘制Python Matplotlib中的一些数据。

该剧情目前: 我的情节目前的样子

生成该图的代码:

groups=['Control','30min','24hour']
cell_lysate_avg=[11887.42595,   4862.429689,    3414.337554]
cell_lysate_sd=[1956.212855,    494.8437915,    525.8556207]
cell_lysate_avg=[i/1000 for i in cell_lysate_avg]
cell_lysate_sd=[i/1000 for i in cell_lysate_sd]


media_avg=[14763.71106,8597.475539,6374.732852]
media_sd=[240.8983759,  167.005365, 256.1374017]
media_avg=[i/1000 for i in media_avg] #to get ng/ml
media_sd=[i/1000 for i in media_sd]

fig, ax = plt.subplots()
index = numpy.arange(len(groups)) #where to put the bars
bar_width=0.45
opacity = 0.5
error_config = {'ecolor': '0.3'}

cell_lysate_plt=plt.bar(index,cell_lysate_avg,bar_width,alpha=opacity,color='black',yerr=cell_lysate_sd,error_kw=error_config,label='Cell Lysates')
media_plt=plt.bar(index+bar_width,media_avg,bar_width,alpha=opacity,color='green',yerr=media_sd,error_kw=error_config,label='Media')
plt.xlabel('Groups',fontsize=15)
plt.ylabel('ng/ml',fontsize=15)
plt.title('\n'.join(wrap('Average Over Biological Repeats for TIMP1 ELISA (n=3)',45)),fontsize=15)
plt.xticks(index + bar_width, groups)
plt.legend()
ax.tick_params(axis='x', labelsize=14)
ax.tick_params(axis='y', labelsize=14)

我已经计算了与此数据相关的各种两个拖尾t检验,并且我想使用标准的科学期刊表示法进行显示-即一条连接两个条形图和一个星形的线,该星形表示的显着性水平为(例如)> 0.05。 有人可以告诉我该怎么做吗?

据我所知,尚无标准的科学期刊可以显示其重要性。 绘制它的确切方式取决于品味。 这可能是为什么matplotlib对显着性条没有特定功能的原因(至少据我所知)。 您可以手动完成。 例如:

from matplotlib.markers import TICKDOWN

def significance_bar(start,end,height,displaystring,linewidth = 1.2,markersize = 8,boxpad  =0.3,fontsize = 15,color = 'k'):
    # draw a line with downticks at the ends
    plt.plot([start,end],[height]*2,'-',color = color,lw=linewidth,marker = TICKDOWN,markeredgewidth=linewidth,markersize = markersize)
    # draw the text with a bounding box covering up the line
    plt.text(0.5*(start+end),height,displaystring,ha = 'center',va='center',bbox=dict(facecolor='1.', edgecolor='none',boxstyle='Square,pad='+str(boxpad)),size = fontsize)

pvals = [0.001,0.1,0.00001]
offset  =1
for i,p in enumerate(pvals):
    if p>=0.05:
        displaystring = r'n.s.'
    elif p<0.0001:
        displaystring = r'***'
    elif p<0.001:
        displaystring = r'**'
    else:
        displaystring = r'*'

    height = offset +  max(cell_lysate_avg[i],media_avg[i])
    bar_centers = index[i] + numpy.array([0.5,1.5])*bar_width
    significance_bar(bar_centers[0],bar_centers[1],height,displaystring)

当然,除了星星,您还可以显式编写p<0.05或类似值。 然后,您可以花费数小时来摆弄参数,直到看起来正确为止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM