Skip to content

01 - plot size

Decreasing Figure Size to Increase Font Size

The first and the most important trick when plotting with Matplotlib is to set the figure size appropriately. If you want to increase the font size and line width everywhere in the figure, the easiest way is to decrease the figure size!

Tip

This is counter-intuitive at first, but it works because Matplotlib scales text elements (like axis labels, tick labels, legends, etc.) relative to the figure size. So a smaller figure size results in larger text elements, making them more readable.

import matplotlib.pyplot as plt
import numpy as np

plt.style.use("physics_plot.pp_base")

x = np.linspace(0, 10, 1000)
y = np.sin(5 * x)

fig, ax = plt.subplots(figsize=(4, 2), constrained_layout=True)
ax.plot(x, y, label=r"$y = \sin(5x)$")
ax.set_ylabel(r"$y(x)$")
ax.set_xlabel(r"$x$")
ax.legend(loc="upper right")
fig.suptitle(r"Small Figure Size (4x2 inches), i.e., \texttt{figsize=(4, 2)}")
fig.savefig("mpl-size-sm.png")

fig, ax = plt.subplots(figsize=(8, 4), constrained_layout=True)
ax.plot(x, y, label=r"$y = \sin(5x)$")
ax.set_ylabel(r"$y(x)$")
ax.set_xlabel(r"$x$")
ax.legend(loc="upper right")
fig.suptitle(r"Large Figure Size (8x4 inches), i.e., \texttt{figsize=(8, 4)}")
fig.savefig("mpl-size-lg.png")

small size large size

DPI

The native figure size unit in Matplotlib is inches as documented here. When we intentionally set a small figure size (e.g., 4x2 inches), we can increase the DPI (dots per inch) to get a larger (higher resolution / size in pixels) image without changing the relative sizes of text elements.

The DPI can be set when creating the figure using the dpi argument:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(4, 2), dpi=600)
or specified when saving the figure using the dpi argument of savefig:

fig.savefig("figure.png", dpi=600)