Why is my matplotlib plot not showing?
Short answer
In a script you usually need plt.show() to display a figure, and it blocks until you close the window. In notebooks the cause is different, and a non-interactive backend shows nothing on screen at all.
The short version
In a plain script, a figure only appears when you call plt.show(), and that call blocks until you close the window. In a notebook, plots usually appear automatically but can be suppressed by a stray semicolon or a missing inline setting. And if matplotlib is using a non-interactive backend (common on servers), nothing appears on screen at all, though you can still save to a file.
In a script: call show
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show() # without this, the script ends and no window appears
plt.show() opens the window and pauses the program until you close it. Leaving it out is the single most common reason a script produces no plot: the figure was built in memory but never displayed.
Only call show once
Call plt.show() a single time, at the end, after building all your plots. Calling it repeatedly, or in the middle of building a figure, can display empty or partial windows. Build everything, then show.
In notebooks
In Jupyter, the last plotting expression in a cell usually renders on its own. Two things commonly hide it. A trailing semicolon suppresses the automatic display of the return value, and while the plot often still appears, the behaviour can be confusing. And on older setups you may need the inline directive.
%matplotlib inline
Modern Jupyter enables inline plotting by default, so you rarely need this now, but it is worth knowing if plots refuse to appear.
On a server: the backend
On a machine with no display (a remote server, a container, a scheduled job), matplotlib falls back to a non-interactive backend, and plt.show() does nothing. This is not a bug. Save the figure to a file instead.
import matplotlib
matplotlib.use("Agg") # non-interactive, file output only
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.savefig("chart.png") # produces a file even with no screen
Set the backend before importing pyplot, and use savefig rather than show. This is the right approach whenever code runs somewhere without a GUI.
The figure closed too soon
If a window flashes and vanishes, the program probably ended immediately after show, or show was not blocking because of the environment. Ensure plt.show() is the last thing the script does, so the window stays until you close it.
A quick checklist
If you see no plot: in a script, confirm you called plt.show() once at the end; in a notebook, remove a trailing semicolon and check inline is on; on a server, switch to saving a file with savefig. One of these three covers almost every case.