Subplots and Figure Size
You can’t draw straight onto a blank Figure — McKinney is explicit about this: “You
can’t make a plot with a blank figure. You have to create one or more subplots.” The
convenient shortcut plt.subplots(nrows, ncols)plt.subplots(nrows, ncols) creates the figure and a NumPy array
of Axes in one call, so you can index into it like axes[0, 1]axes[0, 1] for “row 0, column 1.”
flowchart TD A["plt.subplots(2, 2)"] --> B["fig"] A --> C["axes - a 2x2 array of Axes"] C --> D["axes[0, 0]"] C --> E["axes[0, 1]"] C --> F["axes[1, 0]"] C --> G["axes[1, 1]"] D --> H["Each axes plots independently"]
Subplots
2x2 subplots
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, figsize=(10, 6))
axes[0, 0].plot([1, 2, 3], [1, 4, 9])
axes[0, 0].set_title("Line")
axes[0, 1].bar(["A", "B", "C"], [5, 2, 7])
axes[0, 1].set_title("Bar")
axes[1, 0].hist([1, 1, 2, 2, 2, 3, 4], bins=4, edgecolor="black")
axes[1, 0].set_title("Histogram")
axes[1, 1].scatter([1, 2, 3, 4], [10, 11, 9, 12])
axes[1, 1].set_title("Scatter")
fig.suptitle("Mini dashboard")
plt.tight_layout()
plt.show()2x2 subplots
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, figsize=(10, 6))
axes[0, 0].plot([1, 2, 3], [1, 4, 9])
axes[0, 0].set_title("Line")
axes[0, 1].bar(["A", "B", "C"], [5, 2, 7])
axes[0, 1].set_title("Bar")
axes[1, 0].hist([1, 1, 2, 2, 2, 3, 4], bins=4, edgecolor="black")
axes[1, 0].set_title("Histogram")
axes[1, 1].scatter([1, 2, 3, 4], [10, 11, 9, 12])
axes[1, 1].set_title("Scatter")
fig.suptitle("Mini dashboard")
plt.tight_layout()
plt.show()Figure size rules
- Use larger figures for long labels and multiple subplots.
- Smaller is fine for single simple plots.
Visualize it
A FigureFigure is just the outer frame — each mini panel inside is its own independent
AxesAxes with its own data, ticks, and title:
Tip
plt.tight_layout()plt.tight_layout() prevents label overlaps in most cases.
Next
Continue to Axis Labels and Titles to make every one of those panels self-explanatory.
🧪 Try It Yourself
Exercise 1 – Create a grid of axes
Exercise 2 – Index into the grid
Exercise 3 – Share an axis across subplots
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
