Double event registered on mouse-click if legend is outside axes

后端 未结 3 1529
暗喜
暗喜 2021-01-20 03:21

I have to plot several data sets in one plot. It is useful to be able to highlight one or more of the plots in order to compare them. For this, I toggle the line style of th

3条回答
  •  长发绾君心
    2021-01-20 04:05

    Another approach similar to tcaswell's solution counting events. Only 3 lines added to the original legend picking example. It uses python's function attributes.

    def onpick(event):
        if onpick.count % 2 == 0:    #### LINE ADDED ####  
            # on the pick event, find the orig line corresponding to the
            # legend proxy line, and toggle the visibility
            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.count += 1    #### LINE ADDED ####
    
    onpick.count = 0    #### LINE ADDED ####
    

提交回复
热议问题