Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib "pick_event" not working in embedded graph with FigureCanvasTkAgg

I'm trying to handle some events to perform user interactions with embedded subplots into a Tkinter frame. Like in this example

Works fine with "key_press_event" and "button_press_event", but does not work with "pick_event".

I modified that example from the link, just adding the following piece of code after the mpl_connect calling:

def on_button_press(event):
    print('you pressed mouse button')

canvas.mpl_connect('button_press_event', on_button_press)

def on_pick(event):
    print('you picked:',event.artist)

canvas.mpl_connect('pick_event', on_pick)

Why "pick_event" doesn't work into embedded graphs? And how do get it to work?

My configurations detailed:

  • Windows 10
  • Python 3.5 (conda version)
  • Matplotlib 1.5.3 installed via pip

Thanks in advance!

like image 585
Adailson De Castro Avatar asked Oct 08 '16 20:10

Adailson De Castro


1 Answers

Well, I solved it...

Most events we just need to use mpl_connect method to the magic happen. My mistake is that I didn't notice that we need to say explictly that our plot is "pickable" putting a argument picker=True to only triggers the event if clicked exacly into the artist, and picker=x where x is an integer that is the pixel tolerance for the trigger. So beyond the changes I inserted for pick in the question, we should replace

a.plot(t, s) for a.plot(t, s,picker=True) or a.plot(t, s,picker=10), e.g.

like image 157
Adailson De Castro Avatar answered Sep 19 '22 12:09

Adailson De Castro