Skip to main content
HomeTutorialsPython

Exploring Matplotlib Inline: A Quick Tutorial

Learn how matplotlib inline can enable you to display your data visualizations directly in a notebook quickly and easily! In this article, we cover what matplotlib inline is, how to use it, and how to pair it with other libraries to create powerful visualizations.
Apr 2024

Matplotlib is a powerful library for creating visualizations in Python. When using Jupyter Notebooks, DataLab, or other compatible environments, you can use Matplotlib inline to display Matplotlib plots directly within the notebook without the need for additional windows. For more on Matplotlib's capabilities, check out this tutorial on using the Matplotlib library.

What Does Matplotlib Inline Do?

Matplotlib inline is a backend command in Jupyter Notebooks that enables the rendering of Matplotlib plots directly below code cells. This means that when you execute code that generates Matplotlib plots, the plots are displayed inline within the notebook, rather than in a separate window or output cell.

This inline rendering can be useful when iterating through versions of a plot in a notebook. Since the graph appears directly below the code that generates it, it is easy to see how the changes to your code affect the result. You can use this while applying standard plot features, such as a title, axes labels, a legend, or color coding.

Beware, in some cases, you may run into memory leakage problems, an error where the memory is not freed between plotting events resulting in the figure being incorrectly plotted after changes. If you notice this as a problem, try using the .clf() and .close() methods to clear the memory from the previous plot before replotting.

Important note: In the current versions of Jupyter Notebook, using Matplotlib inline is not necessary as plots are displayed automatically. However, it is still useful to use especially when sharing code. So if you are using the latest version of Jupyter Notebook or are using DataLab, and don’t have code to share, you can skip this tutorial and jump into learning Matplotlib instead.

Matplotlib inline Syntax and Usage with Examples

To enable Matplotlib inline mode in a Jupyter notebook, simply include the following command at the beginning of your notebook:

import matplotlib
%matplotlib inline

This command tells Jupyter to render Matplotlib plots directly in the notebook, as opposed to opening a new window or needing to save an external plot. Once enabled, you can proceed to create and display plots using Matplotlib as usual.

An example of %matplotlib inline in action on a Jupyter Notebook

As mentioned earlier, the latest versions of Jupyter Notebooks & IDEs like DataLab don’t require you to add %matplotlib inline to see plots within the same cell. However, I prefer to add this single line to the top of my scripts even when using such IDEs. It makes it easier to transfer the code between systems and team members. It also reduces the chances of an unexpected error if the IDE stops working the way you expect.

An Interactive Alternative: %matplotlib notebook

If you are looking for interactivity in your inline plots, try the %matplotlib notebook backend command. This command still makes your plots visible within the notebook, and adds some interactivity like zooming, panning, and data inspection. However, I have found this can be temperamental on some systems. If you have trouble with this magic command, I recommend using plotly.io instead for interactive, inline plots.

An example of %matplotlib notebook in action on a Jupyter Notebook

Integrating %matplotlib inline with Other Plotting Libraries

The %matplotlib inline command is not limited to matplotlib plots. It integrates easily with other plotting libraries such as seaborn and plotly, expanding the breadth of plot types you can create inline in your notebook.

Using this command alongside the seaborn library expands the customization options for the plots.

An example of %matplotlib inline with seaborn in action on a Jupyter Notebook

Using this command alongside the plotly library enables interactivity, such as zooming and panning.

An example of %matplotlib inline with plotly in action on a Jupyter Notebook

Key Takeaway: Use the %matplotlib inline command to activate inline plotting in Jupyter notebooks. Pair this command with other plotting libraries to create powerful visualizations.

Best Practices for using %matplotlib inline

When using %matplotlib inline, there are a few best practices to consider:

  1. Add the matplotlib inline command at the top of a notebook, to ensure consistent plot display throughout the notebook.
  2. Be sure to import the matplotlib library before calling the command.
  3. If you want plots to be viewed within the notebook, be sure to use matplotlib inline, even if your IDE defaults to have this behavior. It will ensure smooth functioning, especially when your code is used by a colleague.
  4. Be sure to clear old plots before regenerating new ones to prevent memory leaks.
  5. Make sure the type of plot you use is appropriate to your data and the conclusion you are trying to show.

Conclusion

Matplotlib inline mode is a convenient feature that enhances the usability of Matplotlib in Jupyter notebooks. By enabling inline rendering of plots, it streamlines the workflow of creating, exploring, and communicating insights from data.

To learn more about data visualizations, check out Introduction to Data Visualizations with Matplotlib. Try building your advanced plots and visualizations in this Seaborn course and this tutorial on histograms in Matplotlib.


Photo of Amberle McKee
Author
Amberle McKee

I am a PhD with 13 years of experience working with data in a biological research environment. I create software in several programming languages including Python, MATLAB, and R. I am passionate about sharing my love of learning with the world.

Topics

Continue Your Data Visualization Journey Today!

Course

Introduction to Data Visualization with Matplotlib

4 hr
172.4K
Learn how to create, customize, and share data visualizations using Matplotlib.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

blog

Intermediate Python for Data Science: Matplotlib

Learn to visualize real data with matplotlib's functions.
Karlijn Willems's photo

Karlijn Willems

11 min

cheat sheet

Matplotlib Cheat Sheet: Plotting in Python

This Matplotlib cheat sheet introduces you to the basics that you need to plot your data with Python and includes code samples.
Karlijn Willems's photo

Karlijn Willems

6 min

tutorial

Introduction to Plotting with Matplotlib in Python

This tutorial demonstrates how to use Matplotlib, a powerful data visualization library in Python, to create line, bar, and scatter plots with stock market data.

Kevin Babitz

25 min

tutorial

Line Plots in MatplotLib with Python

This hands-on tutorial dives deep into creating and customizing line plots with Matplotlib, a powerful data visualization library in Python.
Arunn Thevapalan's photo

Arunn Thevapalan

11 min

tutorial

Histograms in Matplotlib

Learn about histograms and how you can use them to gain insights from data with the help of matplotlib.
Aditya Sharma's photo

Aditya Sharma

8 min

tutorial

Python Plotly Express Tutorial: Unlock Beautiful Visualizations

Learn how to create highly interactive and visually appealing charts with Python Plotly Express.
Bekhruz Tuychiev's photo

Bekhruz Tuychiev

10 min

See MoreSee More