Matplotlib从多个系列挑选事件

时间:2017-08-10 18:55:10

标签: python matplotlib picking

我正在做一个散点图,我想点击各个点来做某事。这就像现有的示例代码。

https://matplotlib.org/examples/event_handling/pick_event_demo.html

我已经实现了一个on_pick方法

def on_pick(event):
    ind = event.ind
    for i in ind:
        ...do something here with blue or red data...

但是,我被困了,因为我在同一个情节中放了多个系列(红色和蓝色)

fig, ax = plt.subplots()
ax.set_title('click on a point...')
line, = ax.plot(red_xs, red_ys, 'o', picker=5, color='red') 
line, = ax.plot(blue_xs, blue_ys, 'o', picker=5, color='blue')  

event.ind是一个整数集合。它们是一系列的索引。但是,似乎无法确定它们是哪个系列的索引。

必须有办法做到这一点。有谁知道这个伎俩?

谢谢 彼得

1 个答案:

答案 0 :(得分:2)

您链接到的pick_event_demo可以告诉您如何知道哪条线是哪条线。它说

thisline = event.artist
xdata = thisline.get_xdata()
ydata = thisline.get_ydata()
ind = event.ind

所以ind将索引thisline的所有内容。

提供更全面的例子

line1, = ax.plot(self.red['False'], self.red['True'], 'o', picker=5, color='red') 
line2, = ax.plot(self.blue['False'], self.blue['True'], 'o', picker=5, color='blue') 

dic = {line1 : self.red, line2 : self.blue}

def on_pick(self,event):
    series = dic[event.artist]
    # do something with series
    for i in event.ind:
         print(series[i])


fig.canvas.mpl_connect('pick_event', on_pick)