Table of Contents
The purpose of using “%matplotlib inline” in Python is to display the output of matplotlib plots directly in the code cell of a Jupyter Notebook or IPython shell. This allows for easier and more efficient visualization and analysis of data within the same environment. This command can be used by importing the “matplotlib.pyplot” library and then adding “%matplotlib inline” before running any plot commands. It can also be used in other Python environments, such as Spyder, by adding “%matplotlib inline” at the beginning of the script. Overall, “%matplotlib inline” enhances the functionality and readability of data analysis in Python.
Use “%matplotlib inline” (With Examples)
You can use the following code to display and store plots within a Python Jupyter notebook:
%matplotlib inline
Here’s how this code is described within the :
“With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.”
The following example shows how to use this code in practice.
Example: How to Use %matplotlibe inline
Suppose we attempt to use the following code to create a Matplotlib line plot in a Jupyter notebook:
import matplotlib.pyplotas plt
#define x and y
x = [1, 6, 10]
y = [5, 13, 27]
#attempt to create line plot of x and y
plt.plot(x, y)
Here’s what the output looks like in the Jupyter notebook:

The code runs without any errors, but no line plot is displayed inline with the code.
To fix this, we can use the %matplotlib inline command before we create the line plot:
%matplotlib inline
import matplotlib.pyplotas plt
#define x and y
x = [1, 6, 10]
y = [5, 13, 27]
#create scatter plot of x and y
plt.plot(x, y)Here’s what the output looks like in the Jupyter notebook:

Notice that the code runs without any errors again and the plot is displayed inline in the notebook.
Note that once we’ve used %matplotlib inline, any Matplotlib plots that we create in any future cells in the notebook will also be displayed and store within the notebook.
Additional Resources
The following tutorials explain how to perform other common operations in Python:
Cite this article
stats writer (2024). What is the purpose of using “%matplotlib inline” and how can it be used in Python?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-is-the-purpose-of-using-matplotlib-inline-and-how-can-it-be-used-in-python/
stats writer. "What is the purpose of using “%matplotlib inline” and how can it be used in Python?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/what-is-the-purpose-of-using-matplotlib-inline-and-how-can-it-be-used-in-python/.
stats writer. "What is the purpose of using “%matplotlib inline” and how can it be used in Python?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-is-the-purpose-of-using-matplotlib-inline-and-how-can-it-be-used-in-python/.
stats writer (2024) 'What is the purpose of using “%matplotlib inline” and how can it be used in Python?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-is-the-purpose-of-using-matplotlib-inline-and-how-can-it-be-used-in-python/.
[1] stats writer, "What is the purpose of using “%matplotlib inline” and how can it be used in Python?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. What is the purpose of using “%matplotlib inline” and how can it be used in Python?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
