The code below works exactly as I expect with block=True. It does not work with block=False.
In that case, the plot window is opened, but it just contains garbage from other displayed windows (eg. a pixmap buffer that was never cleared) and the plot is never actually drawn. I assume this is unintended behavior. Does it work properly on other systems? My goal is to draw a plot, and then later update (eg. overwrite) the plot with new data, without requiring user interaction.
I am running Python 3.8.8 on Linux 5.4.0-124-generic #140-Ubuntu (Linux Mint 20 Cinnamon 4.6.7),
and Matplotlib version 3.3.4
import matplotlib.pyplot as plt
import numpy as np
import time
while True:
data = np.random.rand(20)
plt.plot(data)
plt.show(block=True)
time.sleep(2)
Note, following a separate comment here, I also tried adding plt.ion() before the loop. This has exactly the same result as using plt.show(block=False) in that I get a garbage-filled window appear, which is then frozen and my program locks up, the window cannot even be closed with the (X) button at upper right, and I have to stop the Python program. Perhaps this is pointing to a window manager problem(?)
EDIT: I see all three versions of my test code work the same way on Python 3.10.7 running on Windows 10, the only exception being with (block=False) or plt.ion() the window opened is smaller and cleared to all white, before the program gets stuck. So apparently I have some misunderstanding about how this is supposed to work, if in fact it can work at all.
I see this has something to do with running threads. plt.show(block=False) actually works as expected, meaning it displays the plot and then returns immediately without waiting for user interaction, if I type it line-by-line into an interactive session.
However, it does not do this inside a while() loop, even if the while loop is typed into an interactive session. In that case, nothing gets updated on the screen, until the while loop exits, at which point all updates are displayed at once.
It looks like the thing I wanted to do, which is display a continually refreshing plot without user interaction, cannot be simply done in this way.