Matplotlib Plot Not Showing? How to Fix plt.show()

Fix a Matplotlib figure that never appears: a missing plt.show(), the Agg backend, a headless server, Jupyter settings, blank saved files, and windows that close at once.

If a Matplotlib plot isn't showing, first make sure the script calls plt.show(). If it does, print matplotlib.get_backend(): a result of agg means Matplotlib can only write image files and can't open a window.

Quick Example

Run this as a script. It reports the backend in use, then draws a test chart.

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

print("Matplotlib", matplotlib.__version__)
print("Backend:", matplotlib.get_backend())

hour = np.linspace(0, 48, 400)
tide = 2.4 + 1.8 * np.sin(2 * np.pi * hour / 12.42)

fig, ax = plt.subplots(figsize=(8, 3.5), layout="constrained")
ax.plot(hour, tide, color="#0e7490")
ax.set_title("If you can read this, plotting works")
ax.set_xlabel("Hour")
ax.set_ylabel("Tide height (m)")
plt.show()

Test line chart of tide height over 48 hours with the title: If you can read this, plotting works

On a desktop, the backend is typically macosx, qtagg, or tkagg. The behavior described here was checked against Matplotlib 3.11.

Find Your Problem

What you see Likely cause Fix
Script ends, no window, no message No plt.show(), or a headless Linux machine Add plt.show(), or save to a file
FigureCanvasAgg is non-interactive, and thus cannot be shown The Agg backend is active Remove the setting that selects Agg, or install a GUI toolkit
Window flashes and disappears Interactive mode or block=False Block with plt.show() at the end
Saved image is blank savefig() came after plt.show() Save first
Second plt.show() does nothing The figure was already closed Create a new figure
No chart in Jupyter Backend or kernel settings Run %matplotlib inline

Checklist: call plt.show, check the backend, save to a file on a server, save before showing, and use a matplotlib magic command in Jupyter

Add plt.show()

In a .py script, plotting commands only build the figure in memory. plt.show() opens the window and pauses the script until you close it.

plt.plot(hour, tide)
plt.show()  # without this line, a script exits with nothing on screen

Notebooks are the exception: they display figures at the end of each cell.

Fix "FigureCanvasAgg is non-interactive"

The full message is UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown. Agg draws PNG files and has no window. It gets selected in one of these ways, so check each:

  1. The code calls matplotlib.use("Agg"), possibly in an imported module. Search your project for matplotlib.use.
  2. The MPLBACKEND environment variable is set to Agg. Check with echo $MPLBACKEND (echo $env:MPLBACKEND in PowerShell) and unset it.
  3. A matplotlibrc file has a backend line. matplotlib.matplotlib_fname() prints which file is loaded.
  4. No GUI toolkit could be loaded, so Matplotlib fell back to Agg.

For the last case, install a toolkit. Qt works on all major systems:

pip install pyqt6

On Debian or Ubuntu with the system Python, Tk is a separate package:

sudo apt install python3-tk

To request a backend explicitly, call matplotlib.use() before creating any figures. It raises an ImportError that explains what's missing if the backend can't load.

import matplotlib
matplotlib.use("TkAgg")  # or "QtAgg"
import matplotlib.pyplot as plt

Servers, Docker, SSH, and CI

A Linux machine with no display can't open a window. Matplotlib falls back to Agg there, and plt.show() returns without any warning, which makes this the quietest version of the problem. Save the figure instead:

fig, ax = plt.subplots()
ax.plot(hour, tide)
fig.savefig("tide.png", dpi=150, bbox_inches="tight")
plt.close(fig)

Then copy the file to your computer or serve it from your app. The savefig guide covers formats and resolution. To see a window over SSH you need display forwarding, such as ssh -X.

Figure Not Showing in Jupyter

Jupyter uses the inline backend by default, which draws static images under each cell. If something has changed that, select it again:

%matplotlib inline

For zooming and panning, install ipympl, restart the kernel, and use the widget backend:

%pip install ipympl
%matplotlib widget

A call to matplotlib.use("Agg") in the notebook is one way to lose inline plots: plt.show() then raises the non-interactive warning, and displaying a figure object prints only <Figure size 640x480 with 1 Axes>. Remove the call, run %matplotlib inline, and rerun the plotting cell.

Saved Image Is Blank

With a blocking window, plt.show() closes the figure when you dismiss it. A later plt.savefig() creates a new, empty figure and saves that. Save first, or keep a reference and call fig.savefig().

fig, ax = plt.subplots()
ax.plot(hour, tide)
fig.savefig("tide.png")  # before show
plt.show()

Window Closes Immediately

In interactive mode (plt.ion()) or with plt.show(block=False), the script keeps running, and the window goes away when the script ends. plt.pause() gives the window time to draw, and a final blocking call keeps it open:

plt.ion()
fig, ax = plt.subplots()
line, = ax.plot([], [])
ax.set_xlim(0, 48)
ax.set_ylim(0, 4.5)

for end in range(50, 401, 50):
    line.set_data(hour[:end], tide[:end])
    plt.pause(0.1)  # redraws and processes window events

plt.ioff()
plt.show()  # stays open until you close it

For smoother updates, see animating charts in Matplotlib.

Other Causes

  • A Figure() created directly from matplotlib.figure isn't managed by pyplot, so plt.show() ignores it. Use plt.figure() or plt.subplots() when you want a window; see Figure vs Axes.
  • plt.close() before plt.show() removes the figure.
  • Calling GUI functions from a background thread triggers Starting a Matplotlib GUI outside of the main thread will likely fail. Plot from the main thread, or use Agg and save files in the worker.
  • A chart window that opens but looks empty usually means the data was plotted on a different Axes, or the limits exclude it. Check axis limits.

Similar Topics