I am attempting to write a sample chart that updates on every 0.5 seconds.
The code is like so:
import matplotlib.pyplot as plt
import numpy as np
import time
# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, axs = plt.subplots(2, 2)
i = 5
for row in x:
axs[0, 0].plot(x[len(x) - i:], y[len(y) - i:])
axs[0, 0].set_title("main")
axs[1, 0].plot(x, y ** 2)
axs[1, 0].set_title(f"shares {i} with main")
axs[1, 0].sharex(axs[0, 0])
axs[0, 1].plot(x + 1, y + 1)
axs[0, 1].set_title("unrelated")
axs[1, 1].plot(x + 2, y + 2)
axs[1, 1].set_title("also unrelated")
fig.canvas.draw()
fig.canvas.flush_events()
fig.tight_layout()
i += 1
plt.show()
print('THIS NEVER PRINTS - IT SEEMS TO BLOCK ON THE show() CALL ABOVE')
time.sleep(0.5)
If you run it, the charts will be displayed. But that show()
call seems to block, and the loop never executes to update the charts.
How can I make it update?