Styling, subplots and saving
Consistent colours and themes, multi-panel figures that stay readable, and exporting images at the right size.
Themes and colours
plt.style.available[:5]
plt.style.use("seaborn-v0_8-whitegrid") # process-wide default
fig, ax = plt.subplots()
ax.plot(x, y, color="#4f46e5", linewidth=2, linestyle="--")
ax.plot(x, y2, marker="s", markersize=5, alpha=0.8)
cmap = plt.get_cmap("viridis") # perceptually uniform- Use a named style instead of setting twenty properties by hand — consistent output across every chart in a project.
- Perceptually uniform colour maps (viridis, magma) stay readable for colour-blind readers; rainbow maps do not.
- Encode categories with colour and marker or line style: charts get printed in greyscale.
Multiple panels
fig, axes = plt.subplots(1, 2, figsize=(10, 4), sharey=True)
axes[0].plot(x, y)
axes[0].set_title("A")
axes[1].scatter(x, y2)
axes[1].set_title("B")
fig.tight_layout()
# grids with mixed sizes
fig = plt.figure(figsize=(10, 6))
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, (2, 3)) # spans two cells
ax3 = fig.add_subplot(2, 2, 4)Share axes (sharex, sharey) whenever panels use the same units — it removes duplicated tick labels and makes comparison immediate.
Saving and embedding
fig.savefig("chart.png", dpi=150, bbox_inches="tight")
fig.savefig("chart.svg") # vector: scales cleanly
fig.savefig("chart.pdf", transparent=True) # for LaTeX / print
# close when generating many figures in a loop, or memory grows
plt.close(fig)⚠️
When a loop generates hundreds of figures, forgetting
plt.close(fig) leaks memory until the process dies. Close every figure you save.FAQ
PNG or SVG?
PNG for photos and quick sharing; SVG or PDF for anything vector that may be resized, printed, or embedded in a document.
Why is my saved image cropped?
Use
bbox_inches="tight" to include labels that fall outside the default bounding box.Related
Figures, axes and your first plot Grouping, joining and reshaping
Last refreshed 2026-09-18.