当图例在轴外时,选择matplotlib图例

时间:2013-04-28 19:30:37

标签: python matplotlib

当我修改了legend_picking.py示例时,我遇到了有趣的怪癖,其中图例位于轴之外,如下所示:

#leg = ax.legend(loc='upper left', fancybox=True, shadow=True)
leg = ax.legend(loc='upper left', fancybox=True, shadow=True,bbox_to_anchor=(.95,0.95))

onpick函数最终被我在文章中点击的Line2D艺术家调用了两次。我认为这与传说中的儿童“树”有两次线的事实有关。曾经,在包装工的孩子们中,其次,作为传说中的孩子们的名单。

我的工作是修改onpick例程,如下所示:

def onpick(event):
  # on the pick event, find the orig line corresponding to the
  # legend proxy line, and toggle the visibility

  # prevent double pick of same artist all on the same mouseevent
  if onpick.lastevent!=event.mouseevent:
    onpick.artistsdone=[]
  if onpick.lastevent==event.mouseevent and event.artist in onpick.artistsdone:
    return
  onpick.lastevent=event.mouseevent        # save off the mouseevent
  onpick.artistsdone.append(event.artist)  # mark this artist as serviced
  legline = event.artist
  origline = lined[legline]
  vis = not origline.get_visible()
  origline.set_visible(vis)
  # Change the alpha on the line in the legend so we can see what lines
  # have been toggled
  if vis:
      legline.set_alpha(1.0)
  else:
      legline.set_alpha(0.2)
  fig.canvas.draw()
onpick.lastevent=None
onpick.artistsdone=[]

我的问题:有人能想出一种更优雅的方式来解决这个问题吗?   而且,你能想到我不会得到我期望的行为吗?

0 个答案:

没有答案