2017-04-11 17 views
2

ax1=fig1.add_subplot(111)を定義し、関連するlabelの値を持つ8つのデータ系列をプロットした後、次のコード行を使用して凡例を追加しました。Pyplotの凡例インデックスエラー:タプルインデックスが範囲外

ax1.legend(loc='center left', bbox_to_anchor=(1.0, 0.5)) 

私は問題なく何度も前にこのメソッドを使用しているが、この機会に、それは私がなぜこれが起こっている見当がつかない本当に提案をいただければ幸いですIndexError: tuple index out of range

Traceback (most recent call last): 
    File "interface_tension_adhesion_plotter.py", line 45, in <module> 
     ax1.legend(loc='center left', bbox_to_anchor=(1.0, 0.5)) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 564, in legend 
     self.legend_ = mlegend.Legend(self, handles, labels, **kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/legend.py", line 386, in __init__ 
     self._init_legend_box(handles, labels, markerfirst) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/legend.py", line 655, in _init_legend_box 
     fontsize, handlebox)) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/legend_handler.py", line 119, in legend_artist 
     fontsize, handlebox.get_transform()) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/legend_handler.py", line 476, in create_artists 
     self.update_prop(coll, barlinecols[0], legend) 
IndexError: tuple index out of range 

というエラーを生成します。

+0

これはエラーバーのプロットに関連しているようです。しかし、あなたを助けるためには、[mcve]を提供する必要があります。 – ImportanceOfBeingErnest

+1

これは確かにエラーバーでプロットされています: 'ax1.errorbar(tensionsarray、meanproportionsarray、yerr = stdproportionsarray、label =" adhsion = {:02.1f} "フォーマット(l))' – crevell

+0

これは[mcve]ではありません。詳しい情報を提供したい場合は、[編集]をクリックしてください。 – ImportanceOfBeingErnest

答えて

2


1.データが完全でアレイが空でない場合、このコードは完全に機能します。

fig = plt.gcf() 
ax=fig.add_subplot(111) 

for i in range(8): 
    x = np.arange(10) 
    y = i + random.rand(10) 
    yerr = .1*y 
    l = .1*i 
    ax.errorbar(x,y,yerr=yerr,label="adhsion={:02.1f}".format(l)) 

ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5)) 

enter image description here
2.私は私のデータにフィルタを適用し、空の配列を得たとき、私は同じエラーが発生しました。これは次のように再現することができます

fig = plt.gcf() 
ax=fig.add_subplot(111) 

for i in range(8): 
    x = np.arange(10) 
    y = i + random.rand(10) 
    yerr = .1*y 
    l = .1*i 
    if i == 7: 
     ind = np.isnan(y) 
     y = y[ind] 
     x = x[ind] 
     yerr = yerr[ind] 
    ax.errorbar(x,y,yerr=yerr,label="adhsion={:02.1f}".format(l)) 

ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5)) 


このコードは、問題になっているものと同一のトレースバックを提供します。 空の配列がエラーの場合、エラーバーのハンドルが正しくありません。 @crevellで言及


回避策:

handles, labels = ax.get_legend_handles_labels() 
handles = [h[0] for h in handles] 
ax.legend(handles, labels,loc='center left', bbox_to_anchor=(1.0, 0.5)) 


それは動作しますが、伝説はエラーバーラインなしで表示されます。

enter image description here

だから、一つはmatplotlibのをエ​​ラーバー機能に供給されたデータをチェックする必要があります。

関連する問題