IPython
Jupyter Notebook
Matplotlib
Plotting
Inline Plots

How to make IPython notebook matplotlib plot inline

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In an IPython or Jupyter notebook, inline plotting means the figure is rendered directly under the code cell instead of opening in a separate GUI window. The usual way to request that behavior is the %matplotlib inline magic, which tells Matplotlib to use a notebook-friendly backend for the current session.

Enable Inline Output

Place the magic near the top of the notebook, before you create figures.

python
1%matplotlib inline
2
3import numpy as np
4import matplotlib.pyplot as plt

Once that is active, plots created with Matplotlib are displayed in the notebook output area. In many notebook setups this is already the default, but adding the magic keeps the notebook self-explanatory and avoids ambiguity when someone else runs it.

Basic Example

Here is a small, runnable example that generates a simple line plot.

python
1%matplotlib inline
2import numpy as np
3import matplotlib.pyplot as plt
4
5x = np.linspace(0, 2 * np.pi, 200)
6y = np.sin(x)
7
8plt.figure(figsize=(8, 4))
9plt.plot(x, y, label="sin(x)")
10plt.title("Inline Matplotlib Plot")
11plt.xlabel("x")
12plt.ylabel("y")
13plt.legend()
14plt.grid(True)
15plt.show()

The figure appears immediately below the cell. That is the main benefit of inline output: code, result, and explanation stay together in one document.

Why Inline Mode Is Useful

Inline plots are ideal for exploratory analysis, teaching, and reports. When you scroll through the notebook later, the chart is preserved next to the code that produced it. That makes the notebook easier to review and easier to share.

Inline mode is also deterministic compared with external windows. You do not need a desktop GUI session, and you do not need to manage several pop-up windows while iterating on a graph.

Control Figure Appearance

Inline plotting does not change normal Matplotlib APIs. You still control size, labels, styles, and saved files the same way.

python
1%matplotlib inline
2import matplotlib.pyplot as plt
3
4values = [3, 7, 5, 9]
5labels = ["Q1", "Q2", "Q3", "Q4"]
6
7plt.figure(figsize=(6, 4), dpi=140)
8plt.bar(labels, values, color="steelblue")
9plt.title("Quarterly Sales")
10plt.tight_layout()
11plt.show()

If inline figures look blurry, increase dpi or set a larger figure size. That is usually enough for notebooks that will later be exported to HTML or PDF.

Saving Figures Still Works

Displaying a figure inline does not prevent you from saving it.

python
1%matplotlib inline
2import matplotlib.pyplot as plt
3
4plt.plot([1, 2, 3], [1, 4, 9])
5plt.title("Save And Display")
6plt.savefig("plot.png", dpi=150)
7plt.show()

This is useful when the notebook is part of a reproducible workflow and you also need an image artifact for documentation or reports.

Inline Versus Interactive Backends

Inline figures are static. You see the final image, but you do not get built-in pan and zoom controls in the same way as interactive notebook backends. If you need richer interaction, use a notebook backend meant for that environment instead of inline.

For simple data inspection and articles, static output is often the better choice because it is lighter and more portable.

Common Pitfalls

A common mistake is placing %matplotlib inline after plotting code. The magic should be executed before figures are created so the correct backend is active.

Another issue is forgetting that notebooks keep state. If an old figure or style configuration is hanging around, the output can be confusing. Restarting the kernel and rerunning the notebook often clears that up.

It is also easy to assume inline mode automatically makes plots interactive. It does not. Inline mode is mainly for embedded static rendering.

Summary

  • Use %matplotlib inline near the top of the notebook to render plots inside notebook cells.
  • Inline output keeps code and figures together, which is useful for analysis and teaching.
  • Normal Matplotlib functions such as figure, plot, savefig, and show still work.
  • Increase figure size or dpi if notebook images look blurry.
  • Choose an interactive backend only when you actually need in-notebook zooming or panning.

Course illustration
Course illustration

All Rights Reserved.