Non-blocking plotting in Matplotlib: plt.ion, plt.pause and show(block=False)
Codemia Knowledge Hub ·
plt.show() blocks until you close the plot window. To plot without blocking, turn on interactive mode with plt.ion() and call plt.pause() so the GUI can process events; the window stays open and updates while your code keeps running. For a one-shot display, plt.show(block=False) also works, but you still need plt.pause() to keep the window responsive.
What blocking looks like
Method 1: interactive mode with plt.ion()
Interactive mode is the recommended approach for real-time plotting. Matplotlib draws without blocking and accepts updates to existing figures:
The key calls:
plt.ion()enables interactive modefig.canvas.draw_idle()schedules a redrawplt.pause(0.05)processes GUI events and gives the backend time to repaintplt.ioff()andplt.show()at the end keep the final plot visible
Method 2: plt.show(block=False)
When you just want the plot to appear without halting the script:
Without the final plt.pause(), the window may flash and close as soon as the script finishes.
Update artists instead of calling ax.plot() in a loop
Calling ax.plot() on every iteration creates a new Line2D object each time, so objects pile up and the figure slows down. Create the artist once and update its data:
| Approach | Memory | Speed | Recommended |
ax.plot() in every iteration | Grows linearly | Degrades over time | No |
line.set_data() to update | Constant | Consistent | Yes |
ax.clear() then ax.plot() | Constant | Slower (full redraw) | Only if axes change |
canvas.blit() (blitting) | Constant | Fastest | For high frame rates |
Blitting for high frame rates
For dashboards and animations that need more frames per second, blitting redraws only the parts of the canvas that changed:
Blitting skips redrawing the axes, labels and background on every frame. On supported backends it can be 5 to 10 times faster than full redraws.
FuncAnimation for structured animation
FuncAnimation gives production animation code a cleaner structure than a manual loop:
It handles the event loop, timing and blitting for you, and can save the animation to a file:
Keep a reference to the returned object (ani = ...). If it is garbage collected, the animation stops. Also avoid combining it with plt.ion(): FuncAnimation runs its own event loop through plt.show(), and interactive mode can interfere with its timer.
You need an interactive backend
Non-blocking plotting requires a GUI backend:
| Backend | Platform | Set With |
TkAgg | All platforms (default) | matplotlib.use("TkAgg") |
Qt5Agg | All platforms (needs PyQt5) | matplotlib.use("Qt5Agg") |
macosx | macOS native | matplotlib.use("macosx") |
Agg | Headless (no GUI) | matplotlib.use("Agg") |
inline | Jupyter notebooks | %matplotlib inline |
widget | Jupyter (interactive) | %matplotlib widget |
Agg cannot display windows. If plt.show() silently does nothing, check the backend. Call matplotlib.use() before importing matplotlib.pyplot, because the backend is locked once pyplot is imported:
On a headless server with no display server (X11, Wayland or macOS Aqua), interactive windows are not possible. Use Agg and save to files with fig.savefig("plot.png").
Jupyter notebooks
Notebooks behave differently from desktop scripts:
With %matplotlib widget, the plot in the notebook cell is live and can be updated from code.
Keep Matplotlib on the main thread
Matplotlib is not thread-safe. Updating plots from a background thread while the main thread runs the GUI event loop causes crashes or corrupted renders. Produce data in the background and do all plotting on the main thread:
Mistakes that freeze or close the window
- No
plt.pause()aftershow(block=False). The GUI event loop never runs, so the window looks frozen or closes when the script exits. - Calling
ax.plot()in a loop. After thousands of iterations the figure becomes unresponsive. Useline.set_data()orline.set_ydata(). - Setting the backend after importing pyplot.
matplotlib.use()must come first.
Related reading
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.