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.
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.
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.
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.
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 inlinenear 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, andshowstill work. - Increase figure size or
dpiif notebook images look blurry. - Choose an interactive backend only when you actually need in-notebook zooming or panning.

