Graph data over time on the x axis and adjust it in real time using python

I want to build sequential real-time data in time along the x axis. I want the x axis to change dynamically, i.e. When I get new data, the window length should remain the same, but the x axis should shift.

So far, I have managed to display samples of a sample of sequential data, and I cannot change the x axis, but the y axis values ​​continue to change.

I used the code I received from this forum

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 changed the random_gen () part for my case, and I plan the serial data, but I give this code, because otherwise I would give the serial data in a file that only complicates the situation.

I found this code that can be used to search for time values

import datetime

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

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

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

I do not know how to dynamically change the x axis.

python, , , , ,

.

+4
1

, , plt.xlim() , ? , :

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)
0

All Articles