Skillversitas

Plotting in Notebooks

Notebooks render plots inline, which makes them the natural environment for visual exploration. The convention is %matplotlib inline for static plots in the notebook, and a few patterns (saving to files, high-DPI output, interactive backends) cover almost every need.

Static Plots with %matplotlib inline

%matplotlib inline
import matplotlib.pyplot as plt

plt.figure(figsize=(8, 4))
plt.plot([1, 2, 3, 4], [2, 4, 1, 8])
plt.title("Example")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

The figure renders below the cell. The last expression of a cell (a figure, a DataFrame) also auto-renders, so plt.show() can be omitted in many notebooks.

Improving Default Quality

import matplotlib as mpl

mpl.rcParams["figure.dpi"] = 120
mpl.rcParams["axes.grid"] = True
mpl.rcParams["grid.alpha"] = 0.3
mpl.rcParams["font.size"] = 11

Saving to Files

fig, ax = plt.subplots()
ax.bar(["a", "b", "c"], [3, 1, 2])
fig.savefig("output/bar.png", bbox_inches="tight")
fig.savefig("output/bar.svg")  # vector for documents

Save before referencing the image in a Markdown cell: ![bar](output/bar.png).

Seaborn and Pandas Shortcuts

import seaborn as sns

sns.scatterplot(data=df, x="feature", y="target", hue="group")
plt.show()

df.plot.line(x="date", y="value")  # pandas built-in plotting

Interactive Backends (When You Need Them)

  • %matplotlib notebook (legacy) or %matplotlib widget with ipywidgets - zoomable plots in classic notebooks.
  • Plotly: fig.show() renders interactive charts in the browser; fig.write_html("chart.html") for sharing.
  • Bokeh: show(figure) or output_file for app-like interactive plots.

Multi-Panel Layouts

fig, axes = plt.subplots(1, 3, figsize=(12, 3))
for ax, (k, v) in zip(axes, df[["a", "b", "c"]].items()):
    ax.hist(v, bins=20)
    ax.set_title(k)
fig.tight_layout()
plt.show()

Summary

Use %matplotlib inline for static plots that render in the cell, tune rcParams for consistent quality, save figures to files (PNG for the notebook, SVG for documents) before referencing them in Markdown, and reach for plotly, bokeh, or ipywidgets when you need interactivity. Multi-panel layouts with subplots keep comparisons in one figure.