Skip to content

Demystifying %matplotlib inline in Jupyter Notebooks

If you've been tinkering with Python data visualization in Jupyter Notebooks (or using tools like Google Colab), you may have come across a strange line at the top of many notebooks: %matplotlib inline. At first glance, this looks like some kind of magical incantation – and in a way, it is! In this beginner-friendly guide, we'll explain what %matplotlib inline actually does, when to use it, how to use it (with example code), and whether you even need it in modern Jupyter environments. By the end, you'll know exactly why this little "magic" command is so commonly seen and how it makes your Matplotlib plots appear inline (right below your code). So let's dive in!

What is %matplotlib inline?

%matplotlib inline is a special magic command available in IPython and Jupyter notebooks. Magic commands (indicated by the % prefix) are not regular Python code but rather commands that configure the behavior of the IPython kernel (Built-in magic commands — IPython 9.1.0 documentation (opens in a new tab)). In this case, %matplotlib inline tells Jupyter to use Matplotlib's "inline" backend, which means plots will be displayed inline within the notebook output, directly below the code cell that produced them (Exploring Matplotlib Inline: A Quick Tutorial | DataCamp (opens in a new tab)) (Jupyter Notebook Matplotlib Inline: A Beginner's Guide | Saturn Cloud Blog (opens in a new tab)).

In simpler terms, using %matplotlib inline is like telling Jupyter: "Hey, when I create a plot with Matplotlib, please show it right here in the notebook, not in a separate window." Normally (without this setting), Matplotlib might try to open a new window or interactive viewer to show your plot, or you might just see a text representation of the plot object instead of the actual graph (Jupyter Notebook Matplotlib Inline: A Beginner's Guide | Saturn Cloud Blog (opens in a new tab)). The %matplotlib inline command prevents that by embedding the graph output in the notebook page.

Why "inline"? Under the hood, Matplotlib has the concept of different backends (think of a backend as the engine or tool that Matplotlib uses to render graphs) (What does %matplotlib inline do? | Medium (opens in a new tab)). Some backends open an external window to display plots (like a GUI window), while others can display within a Jupyter notebook. The inline backend is one that renders the plot as a static image within the notebook itself (What does %matplotlib inline do? | Medium (opens in a new tab)). By executing %matplotlib inline, you are selecting Matplotlib's inline backend for plotting. This is extremely handy for data analysis, because you typically want your charts and code together in one place (hence the name "inline").

And yes, the % at the beginning is why we call it a "magic" command – it's a special Jupyter-specific command, not part of standard Python syntax (Built-in magic commands — IPython 9.1.0 documentation (opens in a new tab)). You wouldn't put %matplotlib inline in a normal Python script (it would cause a syntax error there), but in a Jupyter Notebook or IPython session, it works its magic to configure plotting.

When (and Why) Should We Use %matplotlib inline?

%matplotlib inline is most useful when you're working in an interactive environment like Jupyter Notebook or JupyterLab and you want to see your plots immediately as output in your notebook. This is the common case for data exploration, data science, and tutorial notebooks. Here are some scenarios and reasons when you would use it:

  • Interactive data analysis: If you're exploring data in a notebook, you'll be plotting a lot of charts to visualize patterns. The inline setting lets you see each plot right under the code that produced it, which makes it easy to compare the code and output. You don't have to switch to another window or interface to see your graph – it appears in line with your code, keeping your flow intact (Jupyter Notebook Matplotlib Inline: A Beginner's Guide | Saturn Cloud Blog (opens in a new tab)) (Jupyter Notebook Matplotlib Inline: A Beginner's Guide | Saturn Cloud Blog (opens in a new tab)). This is especially helpful with iterative work: you can tweak a plotting command and re-run the cell to see the updated chart instantly.

  • Keeping results together: Inline plots make it convenient to share notebooks or keep a record of your analysis. When you save or export the notebook (to HTML or PDF, for example), the charts are embedded as images. This means others can see the plots when viewing your notebook without needing any additional files. Everything (code + plots) is self-contained.

  • Large datasets or complex visualizations: As you work with heavy computations or detailed charts, you might generate multiple plots to understand the data. Inline plotting allows you to stack these visuals in the notebook for easy scrolling and comparison (Jupyter Notebook Matplotlib Inline: A Beginner's Guide | Saturn Cloud Blog (opens in a new tab)). You can intermix explanations (markdown text) between plots, creating a narrative with visual aids – something that's fundamental to the notebook experience.

  • Educational use and tutorials: Many tutorials include %matplotlib inline at the top so that learners will see the output graphs right in the tutorial notebook. It ensures that newbies don't get confused if no plot appears. (After all, nothing is more frustrating than running a plotting command and seeing nothing happen because the plot opened in an unseen window or requires a special command to show!)

On the other hand, if you're not in a Jupyter environment – for example, if you're writing a standalone Python script or using a Python REPL outside of IPython – then %matplotlib inline isn't applicable. In a regular script, you typically create a plot and then call plt.show() to pop up a window with the plot. The %matplotlib inline magic is purely for IPython/Jupyter environments to make plots embed in the notebook. So you wouldn't use it in plain Python scripts (and if you tried, Python would complain because it doesn't know what %matplotlib inline means).

In summary, use %matplotlib inline when you want Matplotlib charts to appear in your notebook, directly under the code, without any extra steps. This is practically always the case when working in Jupyter notebooks for data analysis or visualization tasks.

How to Use %matplotlib inline (Example)

Using %matplotlib inline is very straightforward. You just need to place it in a Jupyter Notebook cell (commonly at the very top of the notebook, after your imports), and it will configure the environment for inline plotting. Typically, you will also import Matplotlib (especially the pyplot module) in the same cell or before plotting. For example:

# At the top of your Jupyter Notebook:
%matplotlib inline  
import matplotlib.pyplot as plt
 
# Now you can create plots as usual
plt.figure(figsize=(4,3))
plt.plot([1, 2, 3], [2, 4, 6], color="blue", marker="o")
plt.title("Example Plot")
plt.xlabel("X values")
plt.ylabel("Y values")
plt.show()

Let's break down what happens here:

  1. %matplotlib inline – this magic command sets up the notebook to show plots in the output below the cell (Jupyter Notebook Matplotlib Inline: A Beginner's Guide | Saturn Cloud Blog (opens in a new tab)). It doesn't produce any visible output by itself; it just changes an internal setting.
  2. Importing matplotlib.pyplot as plt – we import the plotting library (pyplot) as plt for convenience. This is the standard way to use Matplotlib's plotting functions.
  3. Creating a plot – we generate a simple line plot (with some dummy data and labels).
  4. plt.show() – this function call tells Matplotlib to display the figure. In an interactive notebook with inline mode, calling plt.show() is optional (plots often display automatically at the end of the cell), but it's a good habit to include it for clarity. It ensures the plot is rendered at that point.

Now, let's illustrate the difference between not using %matplotlib inline and using it:

  • Without %matplotlib inline: If you do not run the magic command in a Jupyter Notebook, what happens when you plot? In some environments, the plot might try to open in a new window (which may not work in a browser-based notebook), or Jupyter will display only a text representation of the plot object (e.g. something like matplotlib.lines.Line2D object at 0x... as the output) instead of the actual graph (python - Is %matplotlib inline still needed? - Stack Overflow (opens in a new tab)). You would then have to call plt.show() to manually display the figure, and even then it might appear as an external pop-up. This is inconvenient and easy to miss.

  • With %matplotlib inline: When you have this magic enabled, as soon as you create a plot (e.g., by running a cell with plotting commands), the resulting figure will be displayed right below that code cell in the notebook output (Jupyter Notebook Matplotlib Inline: A Beginner's Guide | Saturn Cloud Blog (opens in a new tab)). No separate windows, no manual steps — it just appears in-line. Even if you don't explicitly call plt.show(), Jupyter will output the plot at the end of the cell execution in inline mode. The plot is rendered as an image (PNG by default) embedded in the notebook. This makes it super easy to see the result of your plotting code immediately.

In short, with %matplotlib inline, your plots stay within the notebook; without it, you may not see the plots at all (or they may appear elsewhere). The magic command basically streamlines the display of plots in notebooks (Exploring Matplotlib Inline: A Quick Tutorial | DataCamp (opens in a new tab)). Once enabled, you continue using Matplotlib as usual — the only difference is where the plots show up.

Tip: It's often a good idea to include %matplotlib inline at the very beginning of your notebook (right after your imports). This way, you ensure that all subsequent plotting cells will have plots inlined. Some people also add a semicolon (;) at the end of plotting commands (e.g. plt.plot(...);) to suppress the textual output (the list of Line2D objects) and show only the graphic. This is optional, but it can make your notebook output cleaner by hiding the Python object repr.

Do We Still Need %matplotlib inline Today?

You might be thinking: "If it's so useful, why would we ever stop using it?" The truth is that in modern Jupyter Notebook environments, you often don't need to explicitly call %matplotlib inline – it's sometimes enabled by default. Recent versions of Jupyter notebooks and many IDEs automatically use the inline backend for Matplotlib when you import pyplot or run plotting commands (What does %matplotlib inline do? | Medium (opens in a new tab)) (Exploring Matplotlib Inline: A Quick Tutorial | DataCamp (opens in a new tab)). For example, if you run import matplotlib.pyplot as plt in a new Jupyter Notebook, chances are the notebook backend is already set to inline mode behind the scenes, even if you didn't write the magic command yourself. This is why you might have seen notebooks where plots show up without any %matplotlib inline at the top.

In fact, official documentation and bug reports have noted that using %matplotlib inline is no longer strictly necessary for most users if you're using the standard plotting APIs (like plt.plot or pandas' .plot() methods) (python - Is %matplotlib inline still needed? - Stack Overflow (opens in a new tab)) (What does %matplotlib inline do? | Medium (opens in a new tab)). Simply importing matplotlib or pandas will trigger the inline plotting backend in most Jupyter setups by default. So, if you forget to add it, you might not notice any difference — your plots may still appear inline because the environment took care of it for you.

However, does that mean you should never use it? Not exactly. Here are a few considerations:

  • Compatibility and clarity: Including %matplotlib inline can be seen as a form of documentation for your notebook. It makes it explicit that the plots are intended to appear inline. If someone else runs your notebook on an older setup, or perhaps with a different IPython configuration that doesn't automatically inline plots, having the magic command ensures the plots will show up as intended (Exploring Matplotlib Inline: A Quick Tutorial | DataCamp (opens in a new tab)). It's a one-line "insurance policy" that your visuals will be visible in the notebook. Many people still put it at the top out of habit or to avoid any confusion.

  • Legacy scenarios: If you're using older versions of Jupyter or working in environments like Jupyter Console or certain remote kernels, you might still need to explicitly use the magic to get inline behavior. For example, some cloud notebook services or JupyterLite (a Jupyter running fully in the browser) had differences where %matplotlib inline made a difference in whether a plot displayed without plt.show() (python - Is %matplotlib inline still needed? - Stack Overflow (opens in a new tab)). So it's not 100% obsolete in every scenario.

  • Non-pyplot usage: One edge case mentioned by some developers is if you're using Matplotlib in a very custom way (not via the common pyplot interface). In typical usage (import matplotlib.pyplot as plt), the inline backend is automatic (python - Is %matplotlib inline still needed? - Stack Overflow (opens in a new tab)). But if you bypass pyplot and use the object-oriented interface without ever importing plt, the inline backend might not activate by itself. In such cases, %matplotlib inline could still be needed to embed figures. This is a rare situation for most beginners, though.

  • No harm done: Lastly, having %matplotlib inline in a notebook that doesn't strictly need it does no harm. The worst that happens is that it's redundant. But it doesn't usually slow anything down or cause errors. (One minor note: it can conflict with certain Matplotlib settings if you set them in the same cell after the magic, but this is an advanced detail (python - Is %matplotlib inline still needed? - Stack Overflow (opens in a new tab)). Generally, it's harmless to include.)

Given these points, the answer to "do we still need it?" is: for most modern use cases, you do not absolutely need %matplotlib inline to see your plots, but it's often included anyway for clarity and broad compatibility (Exploring Matplotlib Inline: A Quick Tutorial | DataCamp (opens in a new tab)). Many contemporary learning resources might even omit mentioning it because Jupyter does the right thing by default now. But if you come across it (in older code or other people's notebooks), now you know that it's there to ensure plots are shown inline. And if you're writing a notebook that many others will use, especially in mixed environments, including it at the top can preempt the "Where did my plot go?" questions.

Conclusion

In a nutshell, %matplotlib inline is a little "magic" command that makes your Matplotlib charts appear right below your code in a Jupyter Notebook – no extra steps needed. It was basically a standard line in early Jupyter Notebook workflows to get inline plots. Today, notebooks often inline plots automatically, but the magic command is still commonly seen and used as a safe default.

For beginners, the key takeaways are:

So, the next time you see %matplotlib inline at the top of a notebook, you won't be mystified. You can give a knowing nod, understanding that it's there to make sure your graphs show up where they should. And if you're starting your own data visualization notebook, you can choose to include it for good measure – or confidently skip it if you know your environment already handles inline plotting. Either way, happy plotting, and enjoy the convenience of seeing your beautiful graphs inside your notebook, right next to the code that created them!

Sources: