使用Python进行实时绘图 [英] Realtime plotting in Python

查看:803
本文介绍了使用Python进行实时绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据流,每秒可提供125个浮点,我想实时绘制它们.目前,我的代码如下:

I have a data stream giving me 125 floats per second and I want to plot them live. At the moment my code looks like this:

Code to read data from stream
counter = 0
while True:
    counter = counter+1
    data from stream (x values)

当然,实际上,代码看起来有些复杂,但是我认为这将使建议更加容易.

In reality the code looks a bit more complicated, of course, but this will make giving advice easier, I think.

我正在考虑将图形另存为文件:

I was thinking about just saving the graph as a file:

counter=0
a_data=np.zeros(100,float)                   #this is limited to 100 floats
while True:
    counter = counter+1
    bytestring = sock.recv(51)               # this is the stream data
    raw = struct.unpack(pp,bytestring)       # this is the unpacked data
    twentyfive = (raw[25]-15310)*0.0265      # this is the x value
    a_data[counter] = twentyfive
    plt.plot(a_data)
    print(twentyfive)
    plt.savefig('test.png')
    time.sleep(0.01)

问题在于数据波动很大,因此过于混乱以至于无法提供帮助.该图应向右移动.此外,它还不够快.因此,我在考虑使用pyqtgraph,但在任何在线示例中,我都不知道如何将x值(每秒125微伏值)和y值(由计数器给出的时间步长)馈送到pyqtgraph迄今为止.任何帮助将不胜感激.

The problem is that the data fluctuates a lot so it's way too cluttered to be helpful. The graph should move to the right. In addition it is by no means fast enough. For this reason I was thinking about using pyqtgraph but I have no idea how to feed my x values (125 microvolt values per second) and y values (the time steps as given by the counter) to pyqtgraph in any of the examples I found online so far. Any help would be greatly appreciated.

推荐答案

PyQtGraph在这里是一个不错的选择,实时绘制125个样本/秒应该没问题.您可以使用多种方法来绘制实时数据,滚动数据,并且PyQtGraph中实际上有一个很好的示例文件,该文件仅显示以下内容:

PyQtGraph is a pretty good choice here and it should be no problem to plots 125 samples/second in real time. There are several approaches you could use for plotting real-time, scrolling data and there actually is a good example file in PyQtGraph showing just that: https://github.com/pyqtgraph/pyqtgraph/blob/develop/examples/scrollingPlots.py

您可以通过在安装PyQtGraph之后在Python解释器中运行此示例来运行示例:

You can run the example by running this in your Python interpreter after installing PyQtGraph:

import pyqtgraph.examples
pyqtgraph.examples.run()

,然后选择滚动图"示例.

and selecting the "Scrolling plots" example.

这篇关于使用Python进行实时绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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