How do I plot in real-time in a while loop?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Real-time plotting in Python usually means updating an existing figure repeatedly as new data arrives. The important trick is not to create a brand-new plot inside every loop iteration. Instead, create the figure once, update the line data, and let Matplotlib process GUI events between iterations.
A Simple Real-Time Loop With Interactive Mode
Matplotlib can redraw incrementally if interactive mode is enabled.
This works because the code updates the existing Line2D object instead of rebuilding the figure on every pass.
Why plt.pause Is Often Simpler
In many cases, plt.pause is the easiest way to keep the window responsive. It briefly yields to the GUI event loop and redraws the figure.
For quick scripts, plt.pause is often enough and shorter than calling draw and flush_events manually.
Keeping the Plot Efficient
Real-time plotting slows down when the loop keeps adding new artists. That means code such as ax.plot(...) inside the loop is usually a bad sign. It creates a new line object every iteration and eventually clutters the figure.
Prefer this pattern:
- create axes and artists once
- update data with
set_data - redraw
If you only want a moving window of the latest values, trim the stored data.
This keeps memory usage bounded and the chart focused on recent points.
When FuncAnimation Is Better
If the update loop is really a repeated render callback, matplotlib.animation.FuncAnimation is often a cleaner design than a raw while loop.
This integrates better with the Matplotlib event loop and is often more robust for GUI applications.
Real Data Sources
In real applications, the loop usually reads from a sensor, socket, serial port, or log file. The plotting logic should stay separate from the data acquisition logic so each piece remains easier to test.
If data arrives faster than the screen can refresh, do not redraw on every single sample. Buffer several values and redraw at a lower frequency.
Common Pitfalls
A common mistake is calling plt.plot repeatedly inside the loop. That creates new artists over and over instead of updating the existing one.
Another issue is forgetting to let the GUI event loop run. Without plt.pause, flush_events, or an animation framework, the window may freeze or never update.
Developers also sometimes set fixed axis limits once and then wonder why new data disappears off-screen. If the data range changes, update the limits or call relim and autoscale_view.
Finally, be realistic about refresh speed. Plotting every data point at sensor frequency is often unnecessary and can overwhelm the UI thread.
Summary
- Create the figure once and update existing artists inside the loop.
- Use
plt.pauseor event-loop flushing so the window stays responsive. - Avoid adding a new line on every iteration.
- Use deques or trimming when only recent data should stay visible.
- Consider
FuncAnimationwhen the loop naturally fits an animation callback model.
Related reading
- How do I predict new data's cluster after clustering training data?
- How do I print the full NumPy array, without truncation?
- How do I print the full NumPy array, without truncation?
- How do I read a large csv file with pandas?
- How do I prepend to a short python list?
- How do I prevent Conda from activating the base environment by default?
- How do I read CSV data into a record array in NumPy?
- How do I remove NaN values from a NumPy array?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.