1

I'm writing a script to crop a part of an image in python with matplotlib. The matplotlib documentation about event handling and picking and this question helped me a lot. But they don't use the coordinates afterwards like I would like to do.

The problem is, that my program doesn't wait for clicks in the plot. The program opens the plot, calls mpl_connect and goes on.

How can I fix this problem, so that the program waits for my input.

My code:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-10,10)
y = x**2

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)

global coords
coords = []

def onclick(event):
    ix, iy = event.xdata, event.ydata
    print('x = %d, y = %d'%(ix, iy))

    coords.append((ix, iy))

    if len(coords) == 2:
        fig.canvas.mpl_disconnect(cid)

cid = fig.canvas.mpl_connect('button_press_event', onclick)

I appreciate any help. Thank you very much!

Hierend
  • 61
  • 7
  • are you actually using `plt.show()` somewhere ? you can remove the last line that doesn't help here – PRMoureu Mar 16 '19 at 15:27
  • No, I don't. But the problem would be the same. I have tried the examples from the matplotlib documentation. – Hierend Mar 16 '19 at 15:56
  • this code runs if I add `plt.show()` at the end, and print 2 coordinates – PRMoureu Mar 16 '19 at 15:58
  • It doesn't run when I do the same. I'm using Python 3.5 and matplotlib 2.2.2 in Anaconda with Spyder. Could this be a reason why it doesn't work? – Hierend Mar 16 '19 at 16:21
  • I could imagine the problem being the way you run this code. Is this in IPython? In the terminal/CLI? – ImportanceOfBeingErnest Mar 16 '19 at 16:43
  • I start the program with "Run File"(F5) in Spyder. The output is written in the IPhython console. Because I set the graphics backend to "Automatic", the plot is a new window. – Hierend Mar 16 '19 at 17:35
  • I think the IPython console in Spyder also activates interactive mode. Most probably you can set `plt.ioff()` on top of the script. That would also require to use `plt.show()` at the end. – ImportanceOfBeingErnest Mar 16 '19 at 18:15
  • Unfortunately it doesn't work for me. The program doesn't wait for my input. ): – Hierend Mar 16 '19 at 20:26
  • For me it works if I just put `plt.show()` at the end, use "Automatic" as backend and select "run in current console". The output after pressing F5 and clicking twice in the plot looks [like this](https://i.stack.imgur.com/Oxdyl.png) for me. (Note that I saw your comment by coincidence. If you want to inform someone specifically use @username) – ImportanceOfBeingErnest Mar 16 '19 at 22:09
  • @ImportanceOfBeingErnest thank you for your help! I guess I didn't describe my problem well. I get the same output. But I want to use the coordinates of the mouse clicks afterwards. And that isn't possible. I'm sorry. – Hierend Mar 16 '19 at 22:54
  • The coordinates are then stored in `coords`. Meaning, you can e.g. close the figure and and put some code using `coords` after `plt.show()`. You can also call `plt.close()` instead or after `fig.canvas.mpl_disconnect` to close the figure automatically. You can also put some code in a new function `def afterwards():` and call that function right after `fig.canvas.mpl_disconnect`, keeping the figure open. – ImportanceOfBeingErnest Mar 16 '19 at 22:58

0 Answers0