11
import sys
import matplotlib
import matplotlib.pyplot as plt
print matplotlib.__version__, matplotlib.get_backend()

def hit(event):
  sys.stderr.write('hit\n')

fig = plt.figure()
cid0 = fig.canvas.mpl_connect('key_press_event', hit)
cid1 = fig.canvas.mpl_connect('button_press_event', hit)
print cid0, cid1
plt.show()

With the code above, why can't I have both mouse press event and key press events firing hit? It seems in the order above only the key press events work, whereas if I swap the lines 10 and 11 around (order cid0 and cid1 assignment), then only the mouse events work. I.e. whichever one I connected first hogs the event handler. Is this a built in limitation of matplotlib, or am I trying to connect multiple events in the wrong way?

edit with some extra info: My matplotlib.__version__ is 1.1.0. I have tried with GTKAgg and TkAgg backends with the same result. Using python and ipython, with or without -wthread -pylab, ipython qtconsole --pylab=inline, does not make a difference. The connection ids I get are cid0 == cid1 == 6.

edit 2: My problem still remains today with matplotlib version 1.2.x and TkAgg backend, sys.version 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1]

9
  • Your code works just fine for me. I have tried it with "ipython -pylab" followed by "run yourcode" and "python yourcode.py" -- cheers. I think I am using the GTK backend, but I am not sure. Under what conditions are you running? Commented Aug 1, 2011 at 5:29
  • interesting. could you please tell me your sys.version, matplotlib.backends.backend and your matplotlib.__version__? Commented Aug 1, 2011 at 5:36
  • 1
    Also works fine for me (both print hit) with 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3], Matplotlib version 0.99.1.1 and TkAgg as default backend. Commented Aug 1, 2011 at 6:23
  • 1
    I was wrong, it sure isn't the GTK backend. sys.version => " 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \n [GCC 4.4.5]" matplotlib.backends.backend => TkAgg matplotlib.__version__ => '0.99.3' Commented Aug 1, 2011 at 7:36
  • 2
    i noticed that cid0 == cid1, which smells funny. if i use hit_ = lambda x: hit(x) and then connect one of them to hit_ instead, i get cid0 != cid1 and the event handler is fired for both use-cases as expected. this is the current workaround i will use, but if anyone can get to the root cause of this i would be interested to hear, thanks ! Commented Aug 1, 2011 at 13:44

2 Answers 2

5

I think you stumbled upon this bug: Multiple mpl_connect calls ignored

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I upgraded to 1.3.x recently, and the bug is fixed.
0

I tried your code, and both the actions (mouse and keyboard) did the trick : i had "hit" every time.

I use ubuntu 10.10, python 2.6.6 and matplotlib 0.99.3, all installed by synaptic (not by downloading latest version and running setup.py, as this has lead me into several big problems previously).

I also have python2.6-dev installed, as this adds the missing ".h" headers in most cases.

Hope this helps.​

7 Comments

yes, i have had it working in the past before with the synaptic versions.
Which backend are you using ? I mostly have explicit backend imports, such as : from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as Canvas but when not specified, you use the default from a file : /home/project29/.matplotlib/.matplotlibrc backend : GTKAgg
(too late to complete previous edit) - Also have a look at matplotlib.sourceforge.net/users/customizing.html
it's stated in the comments under the question, i have GTKAgg backend. but i have tried with other, i have also the problem with TkAgg backend.
I have cid=6 for key and cid=7 for mouse - 0.99.3 TkAgg 6 7 hit hit
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.