Visualisation inside notebooks

Inline backends, figure size and resolution, pandas and seaborn output, and interactive libraries that need a renderer.

Choosing a backend

BackendWhere figures appearNotes
%matplotlib inlineStatic PNG in the output cellDefault in modern Jupyter; fast and portable
%matplotlib widgetInteractive canvasNeeds ipympl installed; pan, zoom, read coordinates
%matplotlib notebookInteractive canvasLegacy; prefer widget on current stacks
Plotly notebook rendererInteractive HTML in the cellRequires anywidget or the Plotly extension
%matplotlib inline
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = (7, 3.5)
plt.rcParams["figure.dpi"] = 120          # sharpness in the notebook

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set(xlabel="time (s)", ylabel="throughput", title="Steady state")
fig.tight_layout()
plt.show()

Setting figure.figsize and dpi once in a setup cell is better than repeating them in every plot, and it makes exported images consistent.

pandas and seaborn

import pandas as pd, seaborn as sns

pd.set_option("display.max_columns", 50)
pd.set_option("display.float_format", "{:,.2f}".format)

df.groupby("region")["revenue"].sum().plot.bar()

sns.set_theme(style="whitegrid")
sns.scatterplot(data=df, x="spend", y="revenue", hue="region")
  • The last expression in a cell is rendered as a styled HTML table; wrapping it in print() throws that away.
  • pandas plots return an Axes; capture it when you need to add labels.
  • seaborn's set_theme() changes Matplotlib global state for the whole kernel, so run it early.

Interactive libraries

import plotly.express as px
px.scatter(df, x="spend", y="revenue", color="region",
           hover_data=["campaign"]).show()

# Bokeh
from bokeh.plotting import figure, output_notebook, show
output_notebook()
p = figure(width=500, height=300, title="Latency")
p.line(x, y)
show(p)
⚠️
Interactive figures embed a large JSON payload in the notebook. Two dozen of them can push a file into the tens of megabytes and make it slow to open. Keep the heavy ones out of committed notebooks, or export them to HTML separately.

FAQ

Why do I get a duplicate figure?
A bare figure object is auto-displayed by the notebook, and plt.show() in the same cell can display it again. Pick one: either end the cell with the figure or call plt.show() explicitly.
How do I make plots crisp?
Raise the DPI when displaying and export with a vector format. fig.savefig("plot.svg") or dpi=300, bbox_inches="tight" for PNG gives publication-quality output.

Widgets and interactive output Magics: line, cell and shell commands

Last refreshed 2026-09-18.