Matplotlib
PyCharm
pyplot
UserWarning
plotting

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 TkAgg and QtAgg
  • non-interactive example includes Agg

Check current backend at runtime:

python
import matplotlib
print(matplotlib.get_backend())

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.

python
1import matplotlib
2matplotlib.use("TkAgg")
3
4import matplotlib.pyplot as plt
5
6plt.plot([1, 2, 3], [1, 4, 9])
7plt.title("Interactive Plot")
8plt.show()

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.

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4]
4y = [1, 4, 9, 16]
5
6plt.figure(figsize=(6, 4))
7plt.plot(x, y, marker="o")
8plt.title("Squared Values")
9plt.xlabel("x")
10plt.ylabel("x squared")
11plt.tight_layout()
12plt.savefig("squared-values.png", dpi=150)
13print("saved squared-values.png")

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.

python
1import matplotlib
2
3backend = matplotlib.get_backend().lower()
4if "agg" in backend:
5    raise RuntimeError("Interactive plotting requested but non-interactive backend is active")

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.

bash
python -m pip install --upgrade matplotlib
python -m pip install tk
python -c "import matplotlib; print('backend:', matplotlib.get_backend())"

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 agg warning means your backend cannot open GUI windows.
  • Use an interactive backend for local plotting with show.
  • Use savefig for headless or automated runs.
  • Set backend before importing pyplot.
  • Validate IDE and environment settings when behavior differs between machines.

Course illustration
Course illustration

All Rights Reserved.