python 2,matplotlib 1.1.1中的pylab.ion()和程序运行时的绘图更新 [英] pylab.ion() in python 2, matplotlib 1.1.1 and updating of the plot while the program runs

查看:87
本文介绍了python 2,matplotlib 1.1.1中的pylab.ion()和程序运行时的绘图更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是让脚本计算一些东西,准备一个绘图,并将已经获得的结果显示为pylab.figure-在python 2(特别是python 2.7)中,带有稳定的matplotlib(为1.1.1)

what I am trying to do is having a script compute something, prepare a plot and show the already obtained results as a pylab.figure - in python 2 (specifically python 2.7) with a stable matplotlib (which is 1.1.1).

在python 3(带有matplotlib git build ...版本1.2.x的python 3.2.3)中,这可以正常工作.作为一个简单的示例(通过time.sleep()模拟冗长的计算),请考虑

In python 3 (python 3.2.3 with a matplotlib git build ... version 1.2.x), this works fine. As a simple example (simulating a lengthy computation by time.sleep()) consider

import pylab
import time
import random

dat=[0,1]
pylab.plot(dat)
pylab.ion()
pylab.draw()    
for i in range (18):
    dat.append(random.uniform(0,1))
    pylab.plot(dat)
    pylab.draw()
    time.sleep(1)

在python 2(2.7.3版和matplotlib 1.1.1版)中,代码运行干净,没有错误,但未显示该图. python2解释器的一些尝试和错误似乎建议将pylab.draw()替换为pylab.show();显然,这样做一次就足够了(不像在每次更改/添加绘图后使用draw调用它一样).因此:

In python 2 (version 2.7.3 vith matplotlib 1.1.1), the code runs cleanly without errors but does not show the figure. A little trial and error with the python2 interpreter seemed to suggest to replace pylab.draw() with pylab.show(); doing this once is apparently sufficient (not, as with draw calling it after every change/addition to the plot). Hence:

import pylab
import time
import random

dat=[0,1]
pylab.plot(dat)
pylab.ion()
pylab.show()    
for i in range (18):
    dat.append(random.uniform(0,1))
    pylab.plot(dat)
    #pylab.draw()
    time.sleep(1)

但是,这也不起作用.再次,它运行得很干净,但未显示该图.似乎只有在等待用户输入时才这样做.我不清楚为什么会这样,但是当将raw_input()添加到循环中时,该图终于显示了

However, this doesn't work either. Again, it runs cleanly but does not show the figure. It seems to do so only when waiting for user input. It is not clear to me why this is, but the plot is finally shown when a raw_input() is added to the loop

import pylab
import time
import random

dat=[0,1]
pylab.plot(dat)
pylab.ion()
pylab.show()    
for i in range (18):
    dat.append(random.uniform(0,1))
    pylab.plot(dat)
    #pylab.draw()
    time.sleep(1)
    raw_input()

有了这个,脚本当然会在显示绘图时等待用户输入,并且不会在用户按下Enter之前继续计算数据.当然,这不是故意的.

With this, the script will of course wait for user input while showing the plot and will not continue computing the data before the user hits enter. This was, of course, not the intention.

这可能是由于不同版本的matplotlib(1.1.1和1.2.x)或不同的python版本(2.7.3和3.2.3)引起的.

This may be caused by different versions of matplotlib (1.1.1 and 1.2.x) or by different python versions (2.7.3 and 3.2.3).

有什么方法可以用稳定的(1.1.1)matplotlib用python 2来完成上述脚本(第一个脚本)在python 3的matplotlib 1.2.x中的操作: -在循环或迭代函数中计算数据(在上面的示例中,由time.sleep()模拟需要一段时间),并且 -(仍在计算时)显示先前迭代中已计算的内容 -并不会打扰用户继续按Enter键以使计算继续进行

Is there any way to accomplish with python 2 with a stable (1.1.1) matplotlib what the above script (the first one) does in python 3, matplotlib 1.2.x: - computing data (which takes a while, in the above example simulated by time.sleep()) in a loop or iterated function and - (while still computing) showing what has already been computed in previous iterations - and not bothering the user to continually hit enter for the computation to continue

谢谢;我将不胜感激...

Thanks; I'd appreciate any help...

推荐答案

您希望pause函数为gui框架提供重新绘制屏幕的机会:

You want the pause function to give the gui framework a chance to re-draw the screen:

import pylab
import time
import random
import matplotlib.pyplot as plt

dat=[0,1]
fig = plt.figure()
ax = fig.add_subplot(111)
Ln, = ax.plot(dat)
ax.set_xlim([0,20])
plt.ion()
plt.show()    
for i in range (18):
    dat.append(random.uniform(0,1))
    Ln.set_ydata(dat)
    Ln.set_xdata(range(len(dat)))
    plt.pause(1)

    print 'done with loop'

您不需要每次都创建一个新的Line2D对象,只需更新现有对象中的数据即可.

You don't need to create a new Line2D object every pass through, you can just update the data in the existing one.

文档:

pause(interval)
    Pause for *interval* seconds.

    If there is an active figure it will be updated and displayed,
    and the gui event loop will run during the pause.

    If there is no active figure, or if a non-interactive backend
    is in use, this executes time.sleep(interval).

    This can be used for crude animation. For more complex
    animation, see :mod:`matplotlib.animation`.

    This function is experimental; its behavior may be changed
    or extended in a future release.

使用matplotlib.animate模块是一种真正的过度杀伤力方法.另一方面,如果您愿意的话,它为您提供了一种保存数据的好方法(从我对

A really over-kill method to is to use the matplotlib.animate module. On the flip side, it gives you a nice way to save the data if you want (ripped from my answer to Python- 1 second plots continous presentation).

示例 查看全文

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