0

I am trying to plot a graph for the data being produced using the following code.

import time
import random
import datetime

mylist = []
ct = datetime.datetime.now()

for i in range(0,61):
    x = random.randint(1,100)
    mylist.append(x)

    if len(mylist) == 11:
        right_in_left_out = mylist.pop(0)
    else:
        right_in_left_out = None
    print(mylist)

    time.sleep(1)

I want the graph to show real time plotting and at one time only 10 points should be plotted. The graph should keep moving forward just like how to data is being printed. Almost like an animation.

MJay
  • 35
  • 6
  • Does this answer your question? [Fast Live Plotting in Matplotlib / PyPlot](https://stackoverflow.com/questions/40126176/fast-live-plotting-in-matplotlib-pyplot) – Julien Sep 21 '22 at 05:38
  • Yes, but I need a simple one with only a line plot. Not the heatmap – MJay Sep 21 '22 at 06:02
  • If this works for something complex, it will also work for something easy. It's not like the code there is overcomplicated... – Julien Sep 21 '22 at 06:14

1 Answers1

1

As Julien stated already, the linked complex example is probably what you are looking for.

Taking your code as a basis and assuming that you mixed up x- and y-coordinates, are you looking for something like this?

import time
import random
import datetime

import matplotlib.pyplot as plt

def redraw_figure():
    plt.draw()
    plt.pause(0.00001)

mylist = []
ct = datetime.datetime.now()

#initialize the data
xData = []
x = np.arange(0,10,1)
y = np.zeros(10)

#plot the data
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_ylim([0, 100])
ax.set_xlim([0, 10])
line, = ax.plot(x, y)

for i in range(0,61):
    y = random.randint(1,100)
    mylist.append(y)
    
    if len(mylist) == 11:
        right_in_left_out = mylist.pop(0)
    else:
        right_in_left_out = None
        xData.append(i)
    
    #draw the data
    line.set_ydata(mylist)
    line.set_xdata(xData)
    redraw_figure()

    print(mylist)
    time.sleep(1)
flyakite
  • 724
  • 3
  • 6