2012-07-17 6 views
11

私は棒グラフを使用して各グループのデータを示します。これらのバーのいくつかは互いに大きく異なっています。棒グラフの有意差をどのように示すことができますか?棒グラフの統計的に有意な差を示す

import numpy as np 
import matplotlib.pyplot as plt 
menMeans = (5, 15, 30, 40) 
menStd  = (2, 3, 4, 5) 
ind = np.arange(4) # the x locations for the groups 
width=0.35 
p1 = plt.bar(ind, menMeans, width=width, color='r', yerr=menStd) 
plt.xticks(ind+width/2., ('A', 'B', 'C', 'D')) 

私は複雑なプロットで作業するとき、私はお勧めここに物事のカップルをやった

enter image description here

+0

は、ローカルに隣接して作られているだけの比較はありますか?つまり、 '(A、B)(B、C)(C、D)'の違いだけを表示し、 '(A、C)'の違いは表示しませんか? – Hooked

+0

いいえ、可能なすべてのペアを比較したいと思います。 – imsc

+1

特に、多数のアイテムがある場合は、これをグラフに表示するのが難しい場合があります。 N = 10の場合、アイテムには45種類のペアワイズ比較があります!ペアワイズp値を代わりに行列に表示できるようです。これは効果がありますか? – Hooked

答えて

14

を目指しています。カスタムフォーマットを辞書に引き出すと、パラメータを変更するときの作業が簡単になり、この辞書を複数のプロットに渡すことができます。私は(A,C)の間に注釈を付けることができるボーナスとして、annotateにカスタム関数を書いたことがあります。(私はこれが正しい視覚的アプローチではないという私のコメントによって立っています)。データが変更されたら調整が必要になるかもしれませんが、正しい軌道に乗せる必要があります。

import numpy as np 
import matplotlib.pyplot as plt 
menMeans = (5, 15, 30, 40) 
menStd  = (2, 3, 4, 5) 
ind = np.arange(4) # the x locations for the groups 
width= 0.7 
labels = ('A', 'B', 'C', 'D') 

# Pull the formatting out here 
bar_kwargs = {'width':width,'color':'y','linewidth':2,'zorder':5} 
err_kwargs = {'zorder':0,'fmt':None,'linewidth':2,'ecolor':'k'} #for matplotlib >= v1.4 use 'fmt':'none' instead 

fig, ax = plt.subplots() 
ax.p1 = plt.bar(ind, menMeans, **bar_kwargs) 
ax.errs = plt.errorbar(ind, menMeans, yerr=menStd, **err_kwargs) 


# Custom function to draw the diff bars 

def label_diff(i,j,text,X,Y): 
    x = (X[i]+X[j])/2 
    y = 1.1*max(Y[i], Y[j]) 
    dx = abs(X[i]-X[j]) 

    props = {'connectionstyle':'bar','arrowstyle':'-',\ 
       'shrinkA':20,'shrinkB':20,'linewidth':2} 
    ax.annotate(text, xy=(X[i],y+7), zorder=10) 
    ax.annotate('', xy=(X[i],y), xytext=(X[j],y), arrowprops=props) 

# Call the function 
label_diff(0,1,'p=0.0370',ind,menMeans) 
label_diff(1,2,'p<0.0001',ind,menMeans) 
label_diff(2,3,'p=0.0025',ind,menMeans) 


plt.ylim(ymax=60) 
plt.xticks(ind, labels, color='k') 
plt.show() 

enter image description here

+1

ありがとうございました。非常に有益。 ax.annotate(text、xy =(x、y + 7)、zorder = 10)に 'ax.annotate(text、xy =(X [i]、y + 7)、zorder = 10) p値を中心にする。 – imsc

+0

@imscこれは私が最初に使ったものですが、それはテキストブロックの中心ではなく、テキストブロックの左側の位置です。私にとっては、それはその配置とは少し離れているようです。いずれにしても、あなたはどのように微調整できるかを見て欲しい! – Hooked

+1

ああ、私は '注釈 'に' ha =' center''を入れています。 – imsc

関連する問題