UserWarning Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. when plotting figure with pyplot on Pycharm
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Matplotlib warning about agg means your current backend is non-interactive, so plt.show() cannot open a window. This happens frequently in IDE terminals, remote sessions, and headless environments. The correct fix depends on whether you want interactive windows or file output only.
Understand Interactive vs Non-Interactive Backends
Matplotlib uses a backend to render plots. Interactive backends can display GUI windows, while non-interactive backends render to files.
- interactive examples include
TkAggandQtAgg - non-interactive example includes
Agg
Check current backend at runtime:
If it prints agg, plt.show() will not open a chart window.
Fix for Local Desktop Development
On a local machine with GUI support, switch to an interactive backend before importing pyplot.
In PyCharm, also verify run configuration is not forcing a headless interpreter. If you use scientific mode, PyCharm may route display through its own tools.
Reliable Alternative: Save Figures to Files
When running in CI, Docker, SSH, or notebook export jobs, file rendering is usually the right choice.
This avoids backend issues completely and works on headless systems.
PyCharm-Specific Tips
If warning persists in PyCharm:
- ensure Python environment has GUI toolkit dependencies installed
- disable conflicting plot integration settings if necessary
- run the script from a normal terminal to isolate IDE behavior
You can also create a tiny startup check to fail fast when interactive plotting is required.
This makes failures explicit instead of silently skipping display.
Environment Setup Checklist
Backend errors are often environment problems rather than plotting code bugs. Confirm GUI toolkit packages are installed in the same interpreter used by PyCharm.
If toolkit installation is not possible, move to file-first workflows and integrate generated images into reports or notebooks. Teams running scheduled analytics jobs typically standardize on Agg plus savefig to avoid runtime display dependencies.
When onboarding new developers, include backend expectations in project setup docs. A short setup script that validates backend capability can save hours of troubleshooting.
If your project supports both interactive and batch modes, expose a command-line flag to select behavior explicitly. For example, use --plot-mode=window for local debugging and --plot-mode=file for CI jobs. Making mode selection explicit prevents accidental GUI calls in headless pipelines.
It also makes bug reports clearer because users can include the selected mode and backend in issue details.
That small habit improves triage speed across teams.
Common Pitfalls
A common pitfall is calling matplotlib.use after importing matplotlib.pyplot. Backend selection must happen first.
Another issue is expecting GUI plots from remote servers without display forwarding. In those cases, save images instead.
Developers also forget that different virtual environments can have different toolkit availability. A script that works in one environment may fail in another.
Finally, mixing notebook magics and script backends in the same file can create inconsistent behavior.
Summary
- The
aggwarning means your backend cannot open GUI windows. - Use an interactive backend for local plotting with
show. - Use
savefigfor headless or automated runs. - Set backend before importing
pyplot. - Validate IDE and environment settings when behavior differs between machines.

