I am running a Python script that updates a plot in matplotlib every few seconds. The calculations take several minutes and I would like to be able to pan and zoom the plot in the usual way while it is updating. Is this possible?
Failing that, is it possible to interrupt the script (canceling the rest of the calculation) and then pan/zoom the plot?
I have made the following example. The plot updates very nicely, but you cannot use the pan/zoom tool.
import numpy as np
import matplotlib.pyplot as plt
import time
def time_consuming_calculation():
time.sleep(0.001)
return np.random.normal()
ax = plt.subplot(111)
plt.ion()
plt.show(block=False)
bins = np.linspace(-4,4,100)
data = []
for i in range(0,10000):
print 'Iteration % 4i'%i
data.append(time_consuming_calculation())
if i%1000==0:
n,bin_edges = np.histogram(data,bins=bins)
if i == 0:
line, = plt.plot(bin_edges[:-1],n)
else:
line.set_data(bin_edges[:-1],n)
ax.relim() # Would need to disable this if we can use pan/zoom tool
ax.autoscale()
plt.draw()
plt.ioff()
plt.show()