I am monitoring an experiment in real time using matplotlib to generate plots in a while loop. Ideally, the loop should exit on something like a KeyboardInterrupt
. This works well enough in an Ubuntu test. In Windows 7, using ipython
, it exits with "Terminate batch job (Y/N)?"
then closes the interpreter. I would like to avoid this behavior and leave the interpreter open after the KeyboardInterrupt. Here is a test script.
[EDIT 2]: This script works fine in Windows if ipython
is loaded as ipython --pylab
.
import time
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([0], [0], 'b-o')
window = 50
plot_data = np.zeros((window, 2))
i = 0
start = time.time()
while True:
try:
data = [time.time() - start, np.random.rand()]
print ' '.join('{:.2f}'.format(x) for x in data)
if i < window:
plot_data[i,:] = data
line.set_data(plot_data[0:i+1,0], plot_data[0:i+1,1])
else:
plot_data[0:window-1] = plot_data[1:window]
plot_data[window-1] = data
line.set_data(plot_data[:,0], plot_data[:,1])
ax.relim()
ax.autoscale_view(True,True,True)
fig.canvas.draw()
plt.pause(0.1)
i += 1
except KeyboardInterrupt:
print "Program ended by user.\n"
break
print 'Success!'
[EDIT 1]: I should be more clear why I tagged this with matplotlib. The below example script executes with no problems in either operating system.
i = 0
start = time.time()
while True:
try:
data = [time.time() - start, np.random.rand()]
print ' '.join('{:.2f}'.format(x) for x in data)
time.sleep(0.1)
except KeyboardInterrupt:
print "Proram ended by user. \n"
break
print 'Success!'
All of the packages were installed yesterday as part of a clean installation of Enthought
.
plt.pause
is not interruptible while there is a figure drawn (not a Windows-specific bug). I would recommend bringing this up as a matplotlib Issue – Oswaldooswalt