Titles, labels, and legends

title, xlabel, ylabel, legend, and loc.

A chart without names is a screenshot, not a report. Put the claim in the title, units on the axes, and series in the legend.

Goal

Set title, axis labels, and a legend that does not cover the data.

The three labels

months = ["Jan", "Feb", "Mar", "Apr"]
nairobi = [50, 40, 80, 150]
fig, ax = plt.subplots()
ax.plot(months, nairobi, marker="o", label="Nairobi")
ax.set_title("Nairobi rainfall")
ax.set_xlabel("month")
ax.set_ylabel("mm")
ax.legend()
plt.show()

Legend location

x = np.linspace(0, 2 * np.pi, 80)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), label="sin")
ax.plot(x, np.cos(x), label="cos")
ax.legend(loc="lower left")
ax.set_title("loc=lower left")
plt.show()

Common loc values: best, upper right, upper left, lower left, lower right.

Figure title vs Axes title

x = np.linspace(0, 2 * np.pi, 80)
fig, axes = plt.subplots(1, 2, figsize=(8, 3.2))
axes[0].plot(x, np.sin(x))
axes[0].set_title("sin")
axes[1].plot(x, np.cos(x), color="C1")
axes[1].set_title("cos")
fig.suptitle("Trigonometry")
fig.tight_layout()
plt.show()

suptitle sits above every Axes. Each panel still needs its own set_title.

Tip

label= on plot / bar / scatter feeds the legend. If the legend is empty, you forgot label=.