1

I want to plot some data live so I tried this simple example:

import time
import numpy as np
import matplotlib.pyplot as plt

plt.axis([0, 1000, 0, 1])
plt.ion()
plt.show()

for i in range(1000):
    print "in"
    y = np.random.random()
    plt.scatter(i, y)
    plt.draw()
    time.sleep(0.05)

The plotting window simply opens up, but nothing is ploted on it (the window actually stops responding). I notices it is indeed performing the for cycle since i can se the "in" print output

2
  • try plt.figure() before plt.draw() Commented Jan 25, 2016 at 22:10
  • Hello @threxx , i did that but it actually doesnt work. Only a bunch of fempty pop up. If i indicate which figure to plot int (i.e. plt.figure(1)), still, nothing happens Commented Jan 26, 2016 at 8:32

1 Answer 1

2

I've met problems with Python3 with in-time drawing.
This should work for you.

import numpy as np
import matplotlib.pyplot as plt

plt.axis([0, 1000, 0, 1])
plt.ion()
plt.show()

for i in range(1000):
    print "in"
    y = np.random.random()
    plt.scatter(i, y)
    plt.pause(0.05)
Sign up to request clarification or add additional context in comments.

2 Comments

The reason that pause works where as sleep does not is that pause runs the GUI event loop as well.
@tcaswell , what do you mean by " pause runs the GUI event loop as well "

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.