spyder: matplotlib blocking works in dedicated Python console, not in IPython console

Example code:

import numpy as np
import matplotlib.pyplot as plt

def onclick(event):
    global offset
    offset = event.ydata
    fig.canvas.mpl_disconnect(cid)
    plt.close()
    return

x = np.linspace(0, 2.0 * np.pi, 200)
y = np.sin(x)
fig = plt.figure()
plt.plot(x, y)
plt.title('Mouse left-click on the desired offset')
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show(block=True)

print('Offset =', offset)

When Spyder is configured to run this script in a dedicated Python console, it works fine (plot one sine wave, wait for the mouse click, print the y offset selected).

When Spyder is configured to run this script in an IPython console, there is no plot, and there is a runtime error on the last line – NameError: name ‘offset’ is not defined. This is because the desired blocking on the previous line did not block. Also, when I close the IPython console after running this script, Spyder crashes.

I would like for this example to work correctly in the IPython console because I saw that the Python console will be removed from Spyder 3.2.

  • Spyder Version: 3.1.4dev-201703025 that comes with WinPython 3.5.3.1Qt5-64bit (the same problem happens with Spyder 3.1.2, 3.1.3)
  • Python Version: 3.5.3
  • Qt Version: 5.8.0
  • PyQt Version: 5.8.2 (same problem happens with 5.8.0, 5.8.1)
  • matplotlib: 2.0.0
  • qtconsole: 4.2.1
  • NumPy: 1.11.3
  • Operating system: 64-bit Windows 8.1

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

@hiccup7, what about this?

import numpy as np
import matplotlib.pyplot as plt

def onclick(event):
    global offset
    offset = event.ydata
    fig.canvas.mpl_disconnect(cid)
    plt.close()
    return

x = np.linspace(0, 2.0 * np.pi, 200)
y = np.sin(x)
fig = plt.figure()
plt.plot(x, y)
plt.title('Mouse left-click on the desired offset')
cid = fig.canvas.mpl_connect('button_press_event', onclick)
while not 'offset' in locals():
    plt.pause(5)
print('Offset =', offset)

This doesn’t wait an specific amount of time, but until offset acquires a definite value. I think it’s equivalent to block=True.