问题
Is there a way to plot a set of figures one at a time without closing the previous figure, maintaining control of the UI, and keeping the figures open at the end? Perhaps using a more appropriate backend, or writing it using the OO style instead of the pyplot/pylab style used below?
e.g. I know I can do
plt.ioff()
for i in range(10)
plt.figure()
arr = thisFunctionTakesTenSecondsToGenerateMyArray()
plt.plot(arr)
plt.show()
which will wait for me to close the figure at every iteration
I can also do
plt.ion()
for i in range(10)
plt.figure()
arr = thisFunctionTakesTenSecondsToGenerateMyArray()
plt.plot(arr)
which will plot them as fast as it can generate them (e.g. 10 secs) but will block my entire UI (e.g. I can't even move the windows around), won't show the axes (I only see the figure window), and also will automatically close all the figures at the end of the loop.
The third option that I'm not looking for is this
plt.ioff()
for i in range(10)
plt.figure()
arr = thisFunctionTakesTenSecondsToGenerateMyArray()
plt.plot(arr)
plt.show()
which involves waiting 100 seconds before seeing anything on my screen.
I'm looking for behavior similar to Matlab where I can do
for i = 1:10
figure()
arr = thisFunctionTakesTenSecondsToGenerateMyArray()
plot(arr)
drawnow
which will draw a figure every 10 seconds, and also allow me to move the windows around e.g. if fig 3 is on top and I want to go back to fig 1 while fig 4 is being generated.
Edit: Using Python 2.7.13, Matplotlib 2.0.0. Running it using Spyder 3.1.3 on Windows 7 SP1 - I've tried running it in the built-in IPython console, the vanilla Python console, and from a script all with the same results.
Ideally, I would like to be able to run it both from a script and interactively, i.e. by executing a script or by copying and pasting into the console.
回答1:
just add plt.pause(0.0001)
inside the loop after plt.show(block=False)
, and a final plt.show()
at the end in case you are executing the script from the operating system command line.
来源:https://stackoverflow.com/questions/43420096/plot-one-figure-at-a-time-without-closing-old-figure-matplotlib