【问题标题】:Realtime plotting in PythonPython中的实时绘图
【发布时间】:2015-05-26 14:56:32
【问题描述】:

我有一个数据流,每秒给我 125 个浮点数,我想实时绘制它们。目前我的代码如下所示:

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

当然,实际上代码看起来有点复杂,但我认为这会使提供建议更容易。

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

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迄今为止。任何帮助将不胜感激。

【问题讨论】:

  • 这是一个非常广泛的问题。尽管如此,我认为使用 matplotlib 对于每秒 125 次更新来说太慢了。

标签: python plot real-time live pyqtgraph


【解决方案1】:

PyQtGraph 在这里是一个不错的选择,实时绘制 125 个样本/秒应该没问题。您可以使用多种方法来绘制实时滚动数据,实际上 PyQtGraph 中有一个很好的示例文件显示了这一点:https://github.com/pyqtgraph/pyqtgraph/blob/develop/examples/scrollingPlots.py

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

import pyqtgraph.examples
pyqtgraph.examples.run()

并选择“滚动图”示例。

【讨论】:

    猜你喜欢
    • 2014-09-07
    • 1970-01-01
    • 2013-03-22
    • 2012-03-13
    • 2012-09-22
    • 2019-06-30
    • 2014-10-12
    • 2017-12-16
    • 2013-03-27
    相关资源
    最近更新 更多