How do I plot in real-time in a while loop using matplotlib?

I am trying to plot some data from a camera in real time using OpenCV. However, the real-time plotting (using matplotlib) doesn’t seem to be working.

I’ve isolated the problem into this simple example:

fig = plt.figure()
plt.axis([0, 1000, 0, 1])

i = 0
x = list()
y = list()

while i < 1000:
    temp_y = np.random.random()
    x.append(i)
    y.append(temp_y)
    plt.scatter(i, temp_y)
    i += 1
    plt.show()

I would expect this example to plot 1000 points individually. What actually happens is that the window pops up with the first point showing (ok with that), then waits for the loop to finish before it populates the rest of the graph.

Any thoughts why I am not seeing points populated one at a time?

14 Answers
14

Leave a Comment