0

我有 2 个颜色条,我希望它们都有不同的pickevents 我该怎么做?我的代码自动取款机是

    x_ax.set_picker(5)##colorbar set picker

def on_pick(event):
    artist = event.artist
    val = event.mouseevent.ydata
    xal = event.mouseevent.xdata
    if highlight == 1 :
        print 'hello'
        global highlight
        highlight = 0
        self.canvas.draw()
    if highlight == 0 :
        highlight = 2
        trans = transforms.blended_transform_factory(self.ax.transAxes,self.ax.transData )
        rect = patches.Rectangle((0,1),width=5, height=5, transform=trans, color='yellow',alpha=0.5)
        self.ax.add_patch(rect)
        self.canvas.draw()
        rect.remove()


    else:
        self.canvas.draw()
        trans = transforms.blended_transform_factory(self.ax.transAxes,self.ax.transData )
        rect = patches.Rectangle((0,10),width=25, height=25, transform=trans, color='yellow',alpha=0.5)
        self.ax.add_patch(rect)
        highlight = 1

        self.canvas.draw()
        rect.remove()


self.canvas = FigureCanvas(self, -1, self.fig)
self.canvas.mpl_connect('pick_event',on_pick)

我尝试添加x2_ax.setpicker(5)self.canvas.mpl.connect('pick_event', on_click) 但是当您单击任一颜色栏时,它似乎都同时执行了

我的 on_pick 只是突出显示图表上的一个部分

如何为每个颜色条添加单独的选择事件???

4

1 回答 1

0

您将 pick 事件连接到canvas并且每个canvas. 您需要添加代码on_pick来判断它正在处理的艺术家。您可以使用闭包来做到这一点,一种稍微简单/不太优雅的方法是简单地将 a 添加label到您的轴上

x_ax.set_label('x')
y_ax.set_label('y')

然后在你的on_pick

def on_pick(event):
    #some code here
    art = event.artist
    if isinstance(art, matplotlib.axes.Axes):
        # the artist is as axes
        if art.get_label() == 'x':
            # do one thing
            pass
        elif art.get_label() == 'y':
            # do another thing
            pass

您可以通过使用标签和函数作为值进行键控来做得更好dict,特别是如果您最终有许多标签艺术家。

于 2013-02-19T00:14:49.303 回答