Interactive Matplotlib backends connect plotting code to a desktop or notebook event loop, so figures can open as windows instead of only producing saved files. They matter when a script needs pan, zoom, toolbar controls, cursor readout, or redraw behavior while data is explored.
Matplotlib can choose a backend automatically when a working desktop toolkit is available, but remote shells, containers, and minimal Python installs often fall back to Agg. Agg can save files, while interactive backends such as TkAgg, QtAgg, macosx, GTK4Agg, and WebAgg can display figures through a UI layer.
TkAgg uses Tkinter, a common Python GUI toolkit, and gives a small desktop proof without installing a larger Qt stack. Substitute QtAgg, macosx, GTK4Agg, or another installed backend when that toolkit is the one available in your Python environment.
$ python -c "import tkinter; print('tkinter ready')"
tkinter ready
On minimal Linux systems, install the distribution's Tkinter package, often named python3-tk or similar, before using TkAgg.
import matplotlib matplotlib.use("TkAgg") import matplotlib.pyplot as plt fig, ax = plt.subplots(layout="constrained") ax.plot([1, 2, 3, 4], [2, 5, 3, 6], marker="o") ax.set_title("Interactive backend smoke test") ax.set_xlabel("Run") ax.set_ylabel("Value") plt.show(block=False) plt.pause(3) manager = plt.get_current_fig_manager() print(f"backend: {matplotlib.get_backend()}") print(f"manager: {type(manager).__name__}") print(f"figures: {plt.get_fignums()}") plt.close("all") print(f"closed: {plt.get_fignums()}")
Call matplotlib.use(“TkAgg”) before importing matplotlib.pyplot. If pyplot has already initialized another backend, start a fresh Python process before switching.
$ python interactive_backend.py backend: TkAgg manager: FigureManagerTk figures: [1] closed: []
A remote shell, container, or server session without a forwarded display can still fail to open TkAgg even when Tkinter imports correctly. Use a local desktop session, configured X/Wayland forwarding, or a notebook backend for interactive display.
$ MPLBACKEND=TkAgg python existing_plot.py
MPLBACKEND overrides matplotlibrc backend settings for that process. Avoid exporting it globally in shell startup files unless every Matplotlib process in that account should use the same backend.
plt.show()
$ rm interactive_backend.py