I\'m reading data from a socket in one thread and would like to plot and update the plot as new data arrives. I coded up a small prototype to simulate things but it doesn\'t
import matplotlib.pyplot as plt
import time
import threading
import random
data = []
# This just simulates reading from a socket.
def data_listener():
while True:
time.sleep(1)
data.append(random.random())
if __name__ == '__main__':
thread = threading.Thread(target=data_listener)
thread.daemon = True
thread.start()
#
# initialize figure
plt.figure()
ln, = plt.plot([])
plt.ion()
plt.show()
while True:
plt.pause(1)
ln.set_xdata(range(len(data)))
ln.set_ydata(data)
plt.draw()
If you want to go really fast, you should look into blitting.
f.show()
does not block, and you can use draw
to update the figure.
f = pylab.figure()
f.show()
while True:
time.sleep(1)
pylab.plot(data)
pylab.draw()