0
0
MatplotlibDebug / FixBeginner · 3 min read

How to Fix Figure Not Showing in Matplotlib Quickly

If your matplotlib figure does not show, you likely forgot to call plt.show() after plotting. Adding plt.show() tells matplotlib to display the figure window, which is necessary especially outside interactive environments.
🔍

Why This Happens

Matplotlib creates figures in the background but does not display them automatically in some environments. Without explicitly calling plt.show(), the figure window stays hidden. This often happens in scripts or some IDEs where the plot is not shown unless requested.

python
import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4])
plt.title('Sample Plot')
# Missing plt.show() here
🔧

The Fix

To fix this, add plt.show() at the end of your plotting commands. This function opens the figure window and displays your plot. It is essential when running scripts or non-interactive environments.

python
import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4])
plt.title('Sample Plot')
plt.show()
Output
A window opens displaying a line plot with points 1, 2, 3, 4 on the y-axis and default x-axis.
🛡️

Prevention

Always include plt.show() at the end of your plotting code when working outside interactive environments like Jupyter notebooks. In Jupyter, use %matplotlib inline or %matplotlib notebook magic commands to display plots automatically. Also, avoid running plotting code in environments that suppress GUI windows without proper configuration.

⚠️

Related Errors

  • Empty plot window: Happens if data is empty or plot commands are incorrect; check your data and plot calls.
  • Figure closes immediately: Happens if script ends right after plt.show(); add pauses or run interactively.
  • No plot in Jupyter: Use %matplotlib inline to enable inline plots.

Key Takeaways

Always call plt.show() to display matplotlib figures in scripts.
In Jupyter notebooks, use %matplotlib inline for automatic plot display.
Without plt.show(), figures may be created but not shown.
Check your environment to understand how plots are rendered.
Related errors often involve empty plots or plots closing too fast.