Pyplot图例索引错误:元组索引超出范围。

3
在定义了ax1=fig1.add_subplot(111)并绘制了8个数据系列及其关联的label值之后,我使用了以下代码行添加图例。
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

我不知道为什么会发生这种情况,非常希望得到建议。


这似乎与误差条的绘制有关。但为了帮助您,您需要提供一个MCVE(最小可重现示例)。 - ImportanceOfBeingErnest
1
它确实用误差条绘制:ax1.errorbar(tensionsarray,meanproportionsarray,yerr=stdproportionsarray,label="adhsion={:02.1f}".format(l)) - crevell
这不是一个 [mcve]。如果您想提供更多信息,请[编辑]您的问题。 - ImportanceOfBeingErnest
你提到的误差条已经足够找到解决方法了:通过将误差条从句柄列表中移除,代码就可以正常工作。handles, labels = ax1.get_legend_handles_labels() # 移除误差条 handles = [h[0] for h in handles] ax1.legend(handles, labels,loc='center left', bbox_to_anchor=(1.0, 0.5)) - crevell
如果你找到了答案,有两个选择:(A) 如果你认为这个问题和它的答案对他人有用,你可以自己写一个答案(并在2天后接受它,这样问题就不会继续未解决),或者 (B) 如果你认为这对将来没什么用,你可以删除这个问题。 - ImportanceOfBeingErnest
1个回答

5


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

在此输入图像描述
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 errorbar函数的数据。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接