This article is part of in the series
Published: Friday 16th May 2025

matplotlib inline banner image

Python is fun and powerful as a stand-alone tool, but it gets more powerful and fun when used with other tools. One such tool is Jupyter Notebooks. If you use matplotlib along with Jupyter Notebooks for data visualization, you would have come across this phrase "%matplotlib inline". But what does it actually do? Why is it important? And how does it fit into your Python data science workflow?

This comprehensive blog explains the purpose and functionality of "matplotlib inline", simplifies this magic command for beginners and offers clarity for even experienced Python developers. Get. Set. Let's learn!

What is "matplotlib inline"

The "%matplotlib inline" is a magic command specific to IPython and Jupyter Notebooks that allows matplotlib plots to be displayed directly in the notebook output cells. Without it, plots may appear in a separate window or not display at all, depending on your environment. Let us understand with an example now:

%matplotlib inline
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.title("Simple Line Chart")
plt.show()

When you run this code in a Jupyter Notebook, the plot is rendered just below the cell.

Why Should I Use %matplotlib inline

Here are some reasons why you and I should learn and use this command.

Plot Output Inside Notebook

It ensures that visualizations render inline and directly under the cell where the code is executed.

Better Reproducibility

By keeping visual output next to the code, your notebooks become more readable and reproducible, especially for sharing or debugging.

Smoother Data Science Workflow

Inline plots reduce context-switching, allowing for **rapid experimentation** and analysis.

How does IPython Magic Work

The "%matplotlib" command is a magic function in IPython. There are different backends available:

  • %matplotlib inline: for inline plots (Jupyter Notebooks)
  • %matplotlib notebook: for interactive widgets
  • %matplotlib qt: for external window output

You can run "%lsmagic" in a Jupyter cell to see all available magic commands.

Working of matplotlib inline Explained

When you use "%matplotlib inline", IPython sets up the Agg backend, which is suitable for non-interactive plotting. This backend renders plots as static PNG images and embeds them into the notebook.

It disables interactive features like zooming and panning but is ideal for static analysis, reports, and teaching materials.

Why You Should Not Use It

In a few cases, you are better off with other alternatives. Let us learn more about this. If you're running Python scripts outside Jupyter, such as in a ".py" file from the terminal or an IDE like VS Code or PyCharm, you don't need "%matplotlib inline". Instead, just use:

import matplotlib.pyplot as plt

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

Using %matplotlib inline with Other Libraries

You can combine %matplotlib in line with Pandas, Seaborn, and NumPy for a powerful exploratory data analysis stack. Here is how you can do it:

%matplotlib inline
import seaborn as sns
import pandas as pd

df = sns.load_dataset("iris")
sns.pairplot(df, hue="species")

This visualization will render immediately below the code in your notebook.

%matplotlib inline Alternatives: Interactive Plotting

If you want more control over your visualizations like zoom, pan, and hover, you can use:

%matplotlib notebook

However, notebook mode can be less stable, especially in shared or collaborative notebooks like the Google Colab.

Best Practices and Troubleshooting Instructions

  • Always use "%matplotlib inline" at the start of Jupyter notebooks unless you need interactivity.
  • Don’t use it in ".py" scripts". It is only for notebooks or IPython environments.
  • Avoid mixing "%matplotlib inline" and "%matplotlib notebook" in the same session, as it can lead to unexpected results.
  • Call "plt.show()" explicitly if you're chaining multiple plot commands to control when rendering occurs.
  • If your plot doesn't display it could meand that "%matplotlib inline" is not enabled.  Add "%matplotlib inline" at the top.
  • If the output appears in new window, it indicates that the backend is wrong. Use "%matplotlib inline".
  • If there is no interactivity, use "%matplotlib notebook".

Practical Applications

Here are some use cases where you can use it in real-world applications:

  • Visualize trends, distributions, and correlations in line with your analysis when you work in data analysis.
  • Teach Python plotting concepts with immediate visual feedback.
  • Create reproducible research with embedded charts in Jupyter Notebooks.
  • Prototype static plots before integrating into dynamic dashboards using libraries like Dash or Plotly.

Wrapping Up

The "%matplotlib inline" is a simple yet powerful feature that enhances the experience of working with visual data in Jupyter Notebooks. It provides immediate feedback, keeps your analysis readable, and is indispensable for data science workflows in Python.

Whether you are a beginner plotting your first line chart or a veteran creating complex statistical visuals, understanding how "matplotlib inline" works will make you a more effective Python programmer.

Next Up for You

Python Tabulate: Creating Beautiful Tables from Your Data