Closed
Description
Here is a minimal example where a worker produces a sequence that's plotted every second by the main thread. Between the draw calls, the main thread pauses for 0.001s to update the GUI and sleeps for 1s. This takes 10 seconds. When pausing for 1s and sleeping for 0.001s instead, I would expect this to run in about the same time. However, it takes 70 seconds.
import random
import time
import threading
import matplotlib.pyplot as plt
class Plot:
def __init__(self):
plt.ion()
plt.show()
self.xdata = []
self.ydata = []
self.running = None
self.axis = plt.figure().add_subplot(111)
self.line, = self.axis.plot([])
def start(self):
self.running = True
threading.Thread(target=self.worker).start()
while self.running:
self.axis.set_xlim(0, len(self.xdata))
self.axis.set_ylim(0, max(self.ydata))
self.line.set_data(self.xdata, self.ydata)
plt.draw()
plt.pause(0.001)
time.sleep(1)
def worker(self):
for _ in range(100):
self.xdata.append(len(self.xdata))
self.ydata.append(random.random())
time.sleep(0.1)
self.running = False
if __name__ == '__main__':
start = time.time()
Plot().start()
print(time.time() - start)
Profiling revealed that by far the most time is spent in start_event_loop
of _macosx.FigureCanvas
.
Possibly related: #5251