keepcalculator

06 Numbers and Powers

Matplotlib not showing plots fix the display in scripts jupyter and vs code?

Card No. 06·4712 · Updated · Standard formula, shown below

If your Matplotlib plots aren’t showing up, the fix usually comes down to one of three things: you’re missing a call to plt.show() in a script, you’re using a non-interactive backend, or you’re running code in an environment (like VS Code) that needs a specific backend or setting. This article walks you through the exact steps to get your plots displaying in scripts, Jupyter Notebooks, and VS Code, covering the most common causes and solutions.

Why Matplotlib Plots Don’t Display

Matplotlib is a plotting library that can render figures to a file (like PNG or PDF) or to an interactive window. By default, in a Python script, Matplotlib uses a non-interactive backend, meaning it draws the figure but doesn’t automatically show it. You must explicitly call plt.show() to open a window with your plot. In Jupyter Notebook, the situation is different: the notebook’s own backend (usually inline) renders plots directly below the code cell, so plt.show() is not strictly required (but it doesn’t hurt). In VS Code, the default Python interactive window behaves like Jupyter, but the regular terminal runs scripts like a standard Python environment, so you need the same plt.show() call.

Fixing Display in Regular Python Scripts

If you’re running a script from the terminal or an IDE like PyCharm or VS Code (as a .py file), the most common issue is forgetting plt.show(). Here’s a minimal example:

import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()  # This line opens the window

If you still don’t see a window after adding plt.show(), try these steps:

  • Check if you’re using a virtual environment where matplotlib isn’t installed. Run pip list to verify.
  • Try a different backend. Sometimes the default backend (e.g., TkAgg) fails on certain systems. You can force a backend by adding this at the very top of your script (before importing pyplot):
import matplotlib
matplotlib.use('TkAgg')  # or 'Qt5Agg', 'Agg' for saving only
import matplotlib.pyplot as plt
  • If you’re on a headless server (no GUI), you won’t see a window. Use plt.savefig('plot.png') instead of plt.show().

Fixing Display in Jupyter Notebook and JupyterLab

In Jupyter, plots usually appear automatically because the inline backend is enabled by default. If you don’t see plots, try the following:

  1. Run %matplotlib inline in a cell before your plotting code. This forces the inline backend.
  2. If you want interactive plots (zoom, pan), use %matplotlib notebook (in classic Notebook) or %matplotlib widget (in JupyterLab with ipympl installed).
  3. Restart the kernel if you’ve changed backends mid-session.
  4. Check that you haven’t accidentally set the backend to Agg (non-interactive) in your config. You can see the current backend with matplotlib.get_backend().

If you’re using JupyterLab and inline plots aren’t showing, it might be a browser cache issue. Try hard-refreshing (Ctrl+Shift+R) or clearing the browser cache.

Fixing Display in VS Code

VS Code has two main ways to run Python: the integrated terminal (running a script) and the Python Interactive Window (which mimics Jupyter). For the terminal, the same rules as regular scripts apply: use plt.show(). For the Interactive Window, plots should appear inline, but if they don’t:

  • Make sure the Python extension is installed and you’ve selected the correct interpreter (bottom-left corner).
  • In the Interactive Window, run %matplotlib inline to force inline display.
  • If you’re using the Jupyter extension, ensure it’s up to date.
  • For plots to open in a separate window (instead of inline), you can set "python.dataScience.useNotebookEditor": false in your settings.json, but the default is fine for most.

If you’re running a script in the terminal and the plot window opens but closes immediately, it’s because the script ends and Python exits. Add input("Press Enter to close...") after plt.show() to keep the window open.

Understanding Backends

Matplotlib uses backends to render plots. The backend determines whether plots appear in a window, inline in a notebook, or only as files. Common backends:

  • Agg: Non-interactive, saves to files only. Good for scripts that generate images.
  • TkAgg: Uses Tkinter, works on most systems.
  • Qt5Agg: Uses Qt, requires PyQt5 or PySide2 installed.
  • WebAgg: Displays in a web browser.
  • nbAgg: For Jupyter Notebook, interactive.
  • module://matplotlib_inline.backend_inline: The default for Jupyter, shows static images.

You can check your current backend with matplotlib.get_backend() and change it temporarily with matplotlib.use('backend_name') before importing pyplot. For a permanent change, edit your matplotlibrc file or set the MPLBACKEND environment variable.

Common Problems and Solutions

  • Plot window opens but is blank: This can happen if you call plt.show() before plotting anything. Make sure all plotting commands come before plt.show().
  • Plots appear in Jupyter but not in VS Code terminal: Different backends are used. In the terminal, you need plt.show().
  • Plots appear in VS Code Interactive but not as a separate window: That’s normal; the Interactive Window shows inline by default.
  • Figure not updating in a loop: If you’re updating a plot in a loop, use plt.pause(0.001) to allow the GUI to refresh.
  • ImportError: No module named ‘tkinter’: On Linux, you may need to install python3-tk via your package manager.
  • Plots not showing in Jupyter after upgrading: Try pip install --upgrade matplotlib and restart the kernel.

Frequently Asked Questions

Why do I need to call plt.show() in scripts but not in Jupyter?

In Jupyter, the inline backend automatically renders the plot after the cell executes. In a script, the default backend is non-interactive, so you must explicitly call plt.show() to display the figure in a window. If you don’t, the plot is created but not shown.

How do I keep the plot window open in a script?

After plt.show(), the window stays open until you close it manually. If it closes instantly, it’s because the script ends and Python exits. Add input("Press Enter to exit...") after plt.show() to pause the script.

What does %matplotlib inline do?

It sets the Matplotlib backend to inline for Jupyter, which renders plots as static images directly below the code cell. This is the default in most Jupyter installations. You can also use %matplotlib notebook for interactive plots.

How do I change the Matplotlib backend?

You can change it temporarily in your code with matplotlib.use('backend_name') before importing pyplot. For a permanent change, edit your matplotlibrc file (find it with matplotlib.matplotlib_fname()) and set backend: backend_name, or set the MPLBACKEND environment variable.

Why is my plot not showing in VS Code even with plt.show()?

If you’re running in the terminal, ensure you’re using the correct Python interpreter that has matplotlib installed. Also, try adding matplotlib.use('TkAgg') at the top of your script. If you’re in the Interactive Window, use %matplotlib inline.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →