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.
When I execute this code, the output is:
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:
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:
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.
Here is the output:
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:
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.
- Matplotlib increase plot size
- What is add_axes matplotlib
- Matplotlib Pie Chart Tutorial
- Python Matplotlib tick_params
- Matplotlib unknown projection ‘3d’
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
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.