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.

3
  • Does this answer your question? Fast Live Plotting in Matplotlib / PyPlot Commented Sep 21, 2022 at 5:38
  • Yes, but I need a simple one with only a line plot. Not the heatmap Commented Sep 21, 2022 at 6:02
  • If this works for something complex, it will also work for something easy. It's not like the code there is overcomplicated... Commented Sep 21, 2022 at 6:14

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.