Tôi đang đọc dữ liệu từ một ổ cắm trong một chuỗi và muốn vẽ và cập nhật cốt truyện khi dữ liệu mới đến. Tôi được mã hóa lên một nguyên mẫu nhỏ để mô phỏng điều nhưng nó không hoạt động:Bạn có thể vẽ dữ liệu trực tiếp trong matplotlib không?
import pylab
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()
pylab.figure()
while True:
time.sleep(1)
pylab.plot(data)
pylab.show() # This blocks :(
http://stackoverflow.com/questions/14665636/time-sleep-required-to-keep-qthread-responsive – tacaswell
http://stackoverflow.com/questions/8955869/why-is-plotting-with-matplotlib -so-slow/8956211 # 8956211 – tacaswell
http://stackoverflow.com/questions/11874767/real-time-plotting-in-while-loop-with-matplotlib/15724978#15724978 – tacaswell