当我修改 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=[]
我的问题:有人能想出一种更优雅的方式来处理这个问题吗?并且,你能想到我不会得到我期望的行为的情况吗?