Every figure created by Matplotlib passes through a backend that combines a renderer with either a GUI event loop or a file-only canvas. A desktop script that must open a pan-and-zoom window needs a GUI backend supported by the active Python environment.
The TkAgg backend draws through Tkinter and works well for a small standalone verification script. Select it with matplotlib.use(“TkAgg”) before importing matplotlib.pyplot so the first figure is created with the intended canvas and event loop.
This method targets a standalone Python process in a local desktop session. Notebook kernels manage GUI integration differently, while SSH sessions, containers, and servers without a forwarded display need a file-only or notebook-aware backend instead.
Related: How to set the Matplotlib backend for headless scripts
Related: How to show Matplotlib plots inline in Jupyter
Prerequisite: The Python environment must already provide Tkinter and access to a local desktop display. Package installation varies by Python distribution and remains outside this backend-configuration task.
$ python -m tkinter
A small window should show the installed Tcl/Tk version.
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 check") ax.set_xlabel("Run") ax.set_ylabel("Value")
print(f"backend: {matplotlib.get_backend()}", flush=True) print(f"canvas: {type(fig.canvas).__name__}", flush=True) plt.show()
A blocking plt.show() keeps the GUI event loop responsive until the figure window closes.
$ python interactive_backend.py backend: TkAgg canvas: FigureCanvasTkAgg
A process without access to a desktop display cannot open a TkAgg window. A local session or configured display forwarding is required; otherwise use the related headless or notebook backend.
$ python interactive_backend.py backend: TkAgg canvas: FigureCanvasTkAgg