2

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()

enter image description here

DanHickstein
  • 6,588
  • 13
  • 54
  • 90
  • I'm not sure if this is a duplicate of http://stackoverflow.com/questions/5050988/in-matplotlib-is-there-a-way-to-pop-up-a-figure-asynchronously That question asks if it's possible to show the plot without interrupting the script. The short answer to that is basically to use plt.ion(). The top answer to that question touches on what I'm asking, but I think that it would be interesting to see a solution to this specific example. For instance, I think that perhaps I can include a matplotlib button that will pause the calculation and enable panning+zooming. – DanHickstein Oct 20 '15 at 20:05

0 Answers0