一次绘制一个图形而不关闭旧图形(matplotlib) [英] Plot one figure at a time without closing old figure (matplotlib)

查看:68
本文介绍了一次绘制一个图形而不关闭旧图形(matplotlib)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以一次绘制一组图形,而又不关闭前一个图形,保持对UI的控制以及最后保持图形打开?也许使用更合适的后端,或者使用OO样式而不是下面使用的pyplot/pylab样式编写它?

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?

例如我知道我能做到

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

我也可以

plt.ion()
for i in range(10)
    plt.figure()
    arr = thisFunctionTakesTenSecondsToGenerateMyArray()
    plt.plot(arr)

这将以最快的速度绘制它们(例如10秒),但会阻塞我的整个UI(例如,我什至无法移动窗口),也不会显示轴(我只看到该图)窗口),并且还会在循环结束时自动关闭所有图形.

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

这需要等待100秒,然后才能在屏幕上看到任何内容.

which involves waiting 100 seconds before seeing anything on my screen.

我正在寻找可以做的类似于Matlab的行为

I'm looking for behavior similar to Matlab where I can do

for i = 1:10
    figure()
    arr = thisFunctionTakesTenSecondsToGenerateMyArray()
    plot(arr)
    drawnow

每10秒会绘制一个图形,并且还允许我在例如如果图3位于最上方,而我想在生成图4时返回到图1.

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.

使用Python 2.7.13,Matplotlib 2.0.0.在Windows 7 SP1上使用Spyder 3.1.3运行它-我试过在内置的IPython控制台,原始Python控制台以及从脚本中运行它们都具有相同结果的情况.

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.

推荐答案

只需在 plt.show(block = False) plt.pause(0.0001)>,最后是最后一个 plt.show(),以防您从操作系统命令行执行脚本.

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.

这篇关于一次绘制一个图形而不关闭旧图形(matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆