使用Python创建动态更新图 [英] Create dynamic updated graph with Python

查看:151
本文介绍了使用Python创建动态更新图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助,在Python中编写一个脚本,该脚本将动态地更改数据,数据来源并不重要,屏幕上显示图形。

I need your help to write a script in Python that will take dynamically changed data, the source of data is not matter here, and display graph on the screen.

我知道如何使用matplotlib,但是matplotlib的问题,我可以在脚本结束时只显示一次图形。我需要能够不仅可以一次显示图形,还可以在每次数据更改时立即更新。

I know how to use matplotlib, but the problem with matplotlib, that I can display graph only once, at the end of the script. I need to be able not only to display graph one time, but also update it on the fly, each time when data changes.

我发现可以使用wxPython与matplotlib做这个,但是对我来说这是一个很复杂的一点,因为我根本不熟悉wxPython。

I found that it is possible to use wxPython with matplotlib to do this, but it is little bit complicate to do this for me, because i am not familiar with wxPython at all.

所以我会很高兴,如果有人会向我展示简单的例子,如何用matplotlib使用wxPython来显示和更新简单的图形。
或者,如果这是另外一种方式,这对我来说也会很好。

So I will be very happy if someone will show me simple example how to use wxPython with matplotlib to show and update simple graph. Or, if it is some other way to do this, it will be good to me too.

PS:

好​​的,因为没有人回答并看过matlabllib帮助注意到了@janislaw并写了一些代码。这是一个虚拟的例子:

PS:
Ok, since no one answered and looked at matplotlib help noticed by @janislaw and wrote some code. This is some dummy example:


import time
import matplotlib.pyplot as plt


def data_gen():
    a=data_gen.a
    if a>10:
        data_gen.a=1
    data_gen.a=data_gen.a+1
    return range (a,a+10)

def run(*args):
    background = fig.canvas.copy_from_bbox(ax.bbox)

    while 1:
        time.sleep(0.1)
        # restore the clean slate background
        fig.canvas.restore_region(background)
        # update the data
        ydata = data_gen()
        xdata=range(len(ydata))

        line.set_data(xdata, ydata)

        # just draw the animated artist
        ax.draw_artist(line)
        # just redraw the axes rectangle
        fig.canvas.blit(ax.bbox)

data_gen.a=1
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], animated=True)
ax.set_ylim(0, 20)
ax.set_xlim(0, 10)
ax.grid()

manager = plt.get_current_fig_manager()
manager.window.after(100, run)

plt.show()

此实现有问题,如果您尝试移动窗口,脚本停止。但是基本上可以使用它。

This implementation have problems, like script stops if you trying to move the window. But basically it can be used.

推荐答案

作为matplotlib的替代方法,Chaco库提供了很好的绘图功能,并且在某些方面更好地适合现场绘图。

As an alternative to matplotlib, the Chaco library provides nice graphing capabilities and is in some ways better-suited for live plotting.

查看一些截图示例,链接代码:

See some screenshot examples here with linked code:

http://code.enthought.com/projects/chaco/gallery.php

特别是看到这些例子:

https://github.com/enthought/chaco/blob/2fc52518278c52e81a7e07589bb6aeaaca361770/examples/demo/advanced/data_stream.py

https://github.com/enthought/chaco/blob/2fc52518278c52 e81a7e07589bb6aeaaca361770 / examples / demo / advanced / spectrum.py

Chaco拥有qt和wx的后端,因此它可以很好地处理底层的细节时间。

Chaco has backends for qt and wx, so it handles the underlying details for you rather nicely most of the time.

这篇关于使用Python创建动态更新图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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