Use plt.pause() instead of time.sleep() .
The latter simply executes the main thread, and the GUI event loop does not start. Instead, plt.pause fires an event loop and allows you to interact with the shape.
From the documentation :
Pause for intervals of seconds.
If a shape is active, it will be updated and displayed, and the GUI event outline will work during pause.
If there is no active figure or if the non-interactive backend is in use, this executes time.sleep (interval).
Note
The event loop, which allows you to interact with the drawing, starts only during the pause period. You will not be able to interact with the figure during calculations. If the calculations take a lot of time (say, 0.5 s or more), the interaction will be felt "lags." In this case, it may make sense to allow calculations in a dedicated workflow or process.
source share