title, xlabel, ylabel (Matplotlib)

set_title(), set_xlabel(), and set_ylabel() add the descriptive text to a plot — the heading above it and the names of the x and y axes. They turn a bare chart into one a reader can understand.

Matplotlib

In the object-oriented style you call ax.set_title('Sales by Month'), ax.set_xlabel('Month'), and ax.set_ylabel('Revenue ($)'). The pyplot equivalents are plt.title(), plt.xlabel(), and plt.ylabel(), which act on the current Axes.

These methods accept the same text-styling keywords as any Matplotlib text: fontsize, fontweight, color, and loc to position a title left, center, or right. For a single overall heading across a multi-panel Figure, use fig.suptitle() instead of a per-Axes title.

Clear labels with units are the difference between a chart that communicates and one that merely decorates; they are usually the first thing to add after the data itself.

Syntax

ax.set_title(s)  |  ax.set_xlabel(s)  |  ax.set_ylabel(s)

Examples

Python — Label setters return the Text object they created.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
t = ax.set_title("Revenue")
print(type(t).__name__)

Output:

Text

Learn it in a lesson

See also

Back to the Dictionary