show() (Matplotlib)

plt.show() displays all open Matplotlib figures in a window. In a script it opens an interactive viewer and blocks until you close it; it is the final command that makes your plot actually appear.

Matplotlib

In a standalone script you build the figure with your plotting calls and then call plt.show() once at the end to render it. With the default interactive backend it pops up a window and pauses the program until you close it, so nothing after show() runs until then.

Behavior depends on context. In Jupyter notebooks the inline backend usually renders figures automatically, so show() is often unnecessary; in non-interactive ('Agg') backends used on servers, there is no window to display and you save to a file with savefig instead.

A common pitfall is calling show() inside a loop or before finishing the plot — show typically consumes the figures, so add all your data and labels first, then call it last. Use savefig, not show, when generating images headlessly.

Syntax

plt.show()

Examples

Python — On a server the Agg backend renders to file, not a window.

import matplotlib
matplotlib.use("Agg")  # headless backend
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
# plt.show() opens a window in an interactive backend
print(matplotlib.get_backend())

Output:

Agg

Learn it in a lesson

See also

Back to the Dictionary