Small multiples

City panels.

City panels.

Goal

Run every block and look at the Plot panel.

months = ["Jan", "Feb", "Mar", "Apr"]
series = {
    "Nairobi": [48, 55, 92, 150],
    "Mombasa": [22, 18, 40, 80],
    "Kisumu": [60, 70, 110, 140],
    "Nakuru": [40, 45, 80, 120],
}
fig, axes = plt.subplots(2, 2, figsize=(7, 5), sharey=True)
for ax, (city, vals) in zip(axes.ravel(), series.items()):
    ax.plot(months, vals, marker="o")
    ax.set_title(city)
fig.suptitle("Rainfall mm")
fig.tight_layout()
plt.show()
fig, axes = plt.subplots(1, 2, figsize=(7, 3), sharey=True)
axes[0].bar(["mango", "soda"], [12, 9])
axes[0].set_title("Nairobi")
axes[1].bar(["mango", "soda"], [7, 8])
axes[1].set_title("Mombasa")
plt.show()
print("Shared y-axis keeps the comparison honest.")
x = np.arange(4)
fig, ax = plt.subplots()
ax.plot(x, [48, 55, 92, 150], label="Nairobi")
ax.plot(x, [22, 18, 40, 80], label="Mombasa")
ax.set_xticks(x, ["Jan", "Feb", "Mar", "Apr"])
ax.legend()
plt.title("One panel when series overlap well")
plt.show()
Pitfall

Always plt.show(). Paste the whole editor.