Fix Matplotlib Not Showing Plot in Python

If you’ve ever worked with Matplotlib in Python, you know how efficient and versatile it is for data visualization. But sometimes, after writing your plotting code, you might run into a frustrating issue: your plot just doesn’t show up. I’ve faced this problem many times in my Python development journey, and I want to share some practical ways to solve it quickly.

In this article, I’ll walk you through the common reasons why Matplotlib might not display your plot and how to fix them.

Why Is My Matplotlib Plot Not Showing?

Before getting into solutions, it’s important to understand why this happens. Matplotlib relies on backends to render plots. Sometimes, the backend doesn’t support the display environment you’re running in, or you might have missed a command to explicitly show the plot.

Also, different environments like Jupyter Notebooks, Python scripts, or IDEs handle plotting differently. Knowing this helps you apply the right fix.

Fix Matplotlib Not Showing Plot in Python

Now, I will explain to you the methods to fix Matplotlib not showing a plot in Python.

Check out Matplotlib Legend Font Size

Method 1: Use plt.show() to Display the Plot

One of the simplest yet most overlooked fixes is to explicitly call Python’s show() function from Matplotlib’s pyplot module.

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

You can refer to the screenshot below to see the output.

vscode matplotlib not showing

Without plt.show(), some environments won’t render the plot window. I’ve found that in standalone Python scripts, this call is essential to see the graph pop up.

Read Matplotlib Multiple Plots

Method 2: Use %matplotlib inline in Jupyter Notebooks

If you’re working in a Jupyter Notebook, plots sometimes don’t display because the notebook isn’t set to render plots inline by default.

At the top of your notebook, run:

%matplotlib inline

This magic command tells Jupyter to display plots directly below your code cells. It makes the notebook experience seamless, especially when analyzing data on the fly.

Check out Matplotlib Plots a Line

Method 3: Verify Your Matplotlib Backend

Matplotlib uses different backends to render plots, such as TkAgg, Qt5Agg, or Agg. The Agg backend is non-interactive and won’t show plots on screen, which can be confusing.

To check your current backend, run:

import matplotlib
print(matplotlib.get_backend())

You can refer to the screenshot below to see the output.

plt.show not working

If it returns 'Agg', you are using a non-GUI backend. To fix this, you can switch to an interactive backend like TkAgg:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

Note that you must set the backend before importing pyplot. I’ve found this especially useful when running scripts on servers or remote machines.

Read Python Plot Multiple Lines Using Matplotlib

Method 4: Run Your Script in an Environment with GUI Support

If you’re running your Python script on a headless server or environment without GUI support (like some Linux servers), Matplotlib won’t be able to open plot windows.

In such cases, you can save your plots as image files instead:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.savefig('plot.png')

Later, you can transfer the image file to your local machine to view it. This workaround saved me many times when working remotely.

Check out What is Matplotlib Inline in Python

Method 5: Use %matplotlib notebook for Interactive Plots in Jupyter

If you want interactive plots in Jupyter Notebook (zoom, pan, resize), %matplotlib notebook is a great option:

%matplotlib notebook
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

This enables interactive widgets and improves the data exploration experience, especially useful when analyzing complex datasets.

Method 6: Restart Your Kernel or IDE

Sometimes, the plot window doesn’t show due to a temporary glitch in your Python environment. Restarting the Jupyter kernel, closing and reopening your IDE (like PyCharm or VSCode), or even rebooting your computer can solve the issue.

I recommend this as a quick troubleshooting step before digging deeper.

Read Plot Multiple Lines with Different Colors in Matplotlib

Bonus Tips for a Smooth Plotting Experience

  • Update Matplotlib: Older versions might have bugs or compatibility issues. Use pip install --upgrade matplotlib to get the latest version.
  • Avoid Naming Scripts matplotlib.py: Naming your script the same as a library can cause import conflicts.
  • Check for Conflicting Libraries: Sometimes other visualization libraries can interfere with Matplotlib’s behavior.
  • Use Virtual Environments: Isolate your project dependencies to avoid version conflicts.

Getting your Matplotlib plots to show up is usually straightforward once you know the right tricks. I hope these methods help you overcome the common pitfalls I’ve encountered over the years.

If you want to dive deeper into Matplotlib or data visualization in Python, keep experimenting and exploring different backends and environments. The more you practice, the smoother your workflow will become.

You may like to read other Matplotlib tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.