简体   繁体   English

在x轴上绘制数据时间并使用python实时调整

[英]Plot data against time on x axis and adjust it in real time using python

I want to plot real time serial data against time on x axis. 我想在x轴上绘制实时串行数据与时间的关系。 I want x axis to change dynamically ie when I get new data the window length should remain same but the x axis should shift. 我希望x轴动态变化,即当我获得新数据时,窗口长度应该保持相同,但x轴应该移动。

So far I have been able to plot the serial data sample sample points and I am not able to change the x axis but the y axis values keep on changing. 到目前为止,我已经能够绘制串行数据样本采样点,我无法更改x轴,但y轴值不断变化。

I took the help of the given code which I got from this forum itself 我从这个论坛本身获得了给定代码的帮助

import matplotlib.pyplot as plt
import time
import random
from collections import deque
import numpy as np


def random_gen():
    while True:
        val = random.randint(1,10)
        yield val
        time.sleep(0.1)


a1 = deque([0]*100)
ax = plt.axes(xlim=(0, 20), ylim=(0, 10))
d = random_gen()

line, = plt.plot(a1)
plt.ion()
plt.ylim([0,10])
plt.show()

for i in range(0,20):
    a1.appendleft(next(d))
    datatoplot = a1.pop()
    line.set_ydata(a1)
    plt.draw()
    plt.pause(0.0001) 

I have changed the random_gen() part for my case and I am plotting serial data but I am giving this code because otherwise I would have give to serial data in a file which will only make things complicated. 我已经为我的情况更改了random_gen()部分,我正在绘制串行数据但是我给出了这个代码,因为否则我会在文件中给出串行数据,这只会使事情变得复杂。

I found this code which can be used to find the time values 我发现这个代码可以用来查找时间值

import datetime

dlist = deque([datetime.datetime.now()] * 100)

while 1:
    # collect the data .........

    dlist.appendleft((datetime.datetime.now()))

I have no idea how to change x axis dynamically. 我不知道如何动态更改x轴。

I am new to python and there might be some things that I may be doing wrong so please ignore those 我是python的新手,可能有些东西我可能做错了所以请忽略它们

Please give some suggestions. 请提出一些建议。

Thanks 谢谢

Perhaps I misunderstand your question, but couldn't you use plt.xlim() and update it every time you add new data? 也许我误解了你的问题,但你不能使用plt.xlim()并在每次添加新数据时更新它吗? Using your example the loop would change to: 使用您的示例,循环将更改为:

for i in range(0,20):
    a1.appendleft(next(d))
    datatoplot = a1.pop()
    line.set_ydata(a1)
    plt.xlim(0, 20 + i)
    plt.draw()
    plt.pause(0.0001)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM