Matplotlib not showing plot

In this Python Matplotlib tutorial, we’ll discuss the Matplotlib not showing plot. Here we’ll cover different examples related to the plot not showing using matplotlib. And we’ll also cover the following topics:

  • Matplotlib not showing plot jupyter
  • Matplotlib not showing plot vscode

Matplotlib not showing plot Jupyter

The Jupyter Notebook is a Python-based user interface that allows users to complete Python web server jobs and deposit code solutions by working with an ordered array of input/output cells.

There are two ways to show the plot in jupyter notebook:

  • By using show
  • By using inline

Show() Function

In the matplotlib library, the show() function of the pyplot module is used to show all the figures or plots.

Using inline

We already have a post related to matplotlib inline. To get deep knowledge about matplotlib inline read the article: What is matplotlib inline.

Also, check: module ‘matplotlib’ has no attribute ‘plot’

Matplotlib not showing plot vscode

In this section, we’ll discuss the show function. The show() function in the pyplot module of the matplotlib library is used to display the plots.

The syntax is:

matplotlib.pyplot.show(*args , **kwargs)

The parameters are as follow:

  • args: Specify multiple arguments.
  • kwargs: Specify multiple keyword arguments.

Usually, we’ll use this show() function without an argument.

So let’s see an example for this:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Plot

plt.plot(x, y)

# Display

plt.show()
  • Firstly, we import the matplotlib library with the pyplot submodule for data visualization.
  • Next, we define x and y data coordinates.
  • To plot a simple line graph, we use the plot() function of the pyplot module.
  • In last, to display the plot, we use the show() function. We are using this function without any argument.
READ:  Scipy Find Peaks - Useful Tutorial

When I execute this code, the output is:

matplotlib not showing plot
Matplotlib not showing plot

If we didn’t use this function means show() function to see what will happen.

Code:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Plot

plt.plot(x, y)

If I execute this code now, the output is as follow:

matplotlib plot not showing
Matplotlib plot not showing

It means if we are not using the show() function, it wouldn’t show any plot.

When we use the show() function in the non-interactive mode.

That means when we write the code in the file it will show all the figures or plots and blocks until the plots have been closed.

Let’s understand the concept with an example:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Plot

plt.plot(x, y)

# Display

plt.show()

# Print Statement

print('My First Matplotlib Program')
  • For data visualisation, we first import the matplotlib library with the pyplot submodule.
  • The x and y data coordinates are then defined.
  • The plot() function of the pyplot package is used to create a simple line graph.
  • After this, we use the show() function without any argument to display the plot.
  • Next, we use the print() function to print the statement.

Now, I execute this code to see what will be the output:

matplotlib not showing plot vscode
Matplotlib not showing plot vscode
matplotlib not showing plot terminal
Terminal Output

From the above output, you can see that plot is showing, and in the terminal shell, we can see it is not printing the printing message.

Now, to print the message we have to close the figure. And after closing the figure message will print in the terminal shell.

READ:  Python Turtle Polygon

Here is the output:

matplotlib not showing plot windows
Message Print

Basically, it will block the execution of the statement until the figure has been closed.

If you want to change that, then you have to take a block as a parameter in the show() method.

Block is a boolean value you can set to True or False. And the default value of the block is True.

The following is the syntax:

matplotlib.pyplot.show(block = True | False)

Block is nothing, but it will block the execution of the code until we close all the plots.

If you take a block argument as False, then it would not block the execution. It means that it shows the printing message with the open plot.

Let’s see an example:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Plot

plt.plot(x, y, color='green', linewidth=3, linestyle='dotted')

# Display

plt.show(block=False)

# Print Statement

print('My First Matplotlib Program')

# Hold the plot

plt.pause(1000)

The output is as below:

matplotlib not showing plot spyder
plt.show(block=False)

If the plot blinks and closes when you set the block argument to False. Don’t worry use the plt.pause() function to hold the plot.

The show() function is used in all the editors and operating systems such as [ colab, pycharm, mac, ubuntu, spyder, Linux, terminal, jupyter lab ] to show the plots.

You may also like to read the following Matplotlib tutorials.

So, in this Python tutorial, we have discussed the “Matplotlib not showing plot” and we have also covered some examples related to using plot not showing matplotlib. These are the following topics that we have discussed in this tutorial.

  • Matplotlib not showing plot jupyter
  • Matplotlib not showing plot vscode