Matplotlib Title and Label Functions
Matplotlib Reference Documentation
Matplotlib provides a set of functions for setting chart titles, axis labels, and figure suptitles, forming the foundation for making charts complete and readable.
Functions Involved
| Function | Purpose | Corresponding Axes Method |
|---|---|---|
| title() | Set Axes Title | ax.set_title() |
| xlabel() / ylabel() | Set x/y Axis Labels | ax.set_xlabel() / ax.set_ylabel() |
| suptitle() | Set Figure Suptitle | fig.suptitle() |
| supxlabel() / supylabel() | Set Figure-level x/y Axis Labels | fig.supxlabel() / fig.supylabel() |
Function Definition
matplotlib.pyplot.title(label, fontdict=None, loc=None, pad=None,
*, y=None, **kwargs)
matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, *,
loc=None, **kwargs)
matplotlib.pyplot.ylabel(ylabel, fontdict=None, labelpad=None, *,
loc=None, **kwargs)
matplotlib.pyplot.suptitle(t, **kwargs)
matplotlib.pyplot.supxlabel(t, **kwargs)
matplotlib.pyplot.supylabel(t, **kwargs)
Common Parameters
| Parameter | Description |
|---|---|
| label / xlabel / ylabel / t | Text content (required) |
| fontsize / size | Font size, e.g., 14, 'large', 'x-large' |
| fontweight / weight | Font weight: 'normal', 'bold', 'light' |
| color | Text color |
| fontdict | Font properties dictionary, e.g., {'fontsize':14, 'color':'blue'} |
| loc | Alignment: 'center' (default), 'left', 'right' |
| pad / labelpad | Padding between text and axes/chart edge (in points) |
| y (title-specific) | Vertical position of the title (in Axes coordinates, 1.0 is the top) |
AxesObject method naming starts withset_prefix, such asax.set_title()、ax.set_xlabel(), which is the recommended approach for the object-oriented interface.
Usage Examples
Example 1: Basic Title and Labels
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x) * np.exp(-x/5)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.plot(x, y, 'steelblue', linewidth=2)
# Set title (Axes level)
ax.set_title('Damped Sine Wave', fontsize=14, fontweight='bold')
# Set axis labels
ax.set_xlabel('Time (seconds)', fontsize=12)
ax.set_ylabel('Amplitude (V)', fontsize=12)
ax.grid(True, alpha=0.3)
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x) * np.exp(-x/5)
fig, ax = plt.subplots(figsize=(8, 4), layout='constrained')
ax.plot(x, y, 'steelblue', linewidth=2)
# Set title (Axes level)
ax.set_title('Damped Sine Wave', fontsize=14, fontweight='bold')
# Set axis labels
ax.set_xlabel('Time (seconds)', fontsize=12)
ax.set_ylabel('Amplitude (V)', fontsize=12)
ax.grid(True, alpha=0.3)
plt.show()
Example 2: Figure Suptitle + Axis Labels
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, axes = plt.subplots(2, 2, figsize=(10, 8),
layout='constrained')
# Each subplot has its own title
axes[0, 0].plot(x, np.sin(x)); axes[0, 0].set_title('sin(x)')
axes[0, 1].plot(x, np.cos(x)); axes[0, 1].set_title('cos(x)')
axes[1, 0].plot(x, np.sin(2*x)); axes[1, 0].set_title('sin(2x)')
axes[1, 1].plot(x, np.cos(2*x)); axes[1, 1].set_title('cos(2x)')
# Figure-level suptitle
fig.suptitle('Trigonometric Functions Overview',
fontsize=16, fontweight='bold', y=1.02)
# Figure-level axis labels (centered across all subplots)
fig.supxlabel('x (radians)', fontsize=13)
fig.supylabel('Amplitude', fontsize=13)
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
fig, axes = plt.subplots(2, 2, figsize=(10, 8),
layout='constrained')
# Each subplot has its own title
axes[0, 0].plot(x, np.sin(x)); axes[0, 0].set_title('sin(x)')
axes[0, 1].plot(x, np.cos(x)); axes[0, 1].set_title('cos(x)')
axes[1, 0].plot(x, np.sin(2*x)); axes[1, 0].set_title('sin(2x)')
axes[1, 1].plot(x, np.cos(2*x)); axes[1, 1].set_title('cos(2x)')
# Figure-level suptitle
fig.suptitle('Trigonometric Functions Overview',
fontsize=16, fontweight='bold', y=1.02)
# Figure-level axis labels (centered across all subplots)
fig.supxlabel('x (radians)', fontsize=13)
fig.supylabel('Amplitude', fontsize=13)
plt.show()
Example 3: Title Position and Style
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# Left plot: left-aligned title
ax1.plot(x, np.sin(x), 'steelblue')
ax1.set_title('Left-aligned Title',
loc='left', # Left-aligned
fontsize=12,
color='steelblue')
ax1.set_xlabel('X-axis', fontsize=10, color='gray')
# Right plot: right-aligned title
ax2.plot(x, np.cos(x), 'coral')
ax2.set_title('Right-aligned Title',
loc='right', # Right-aligned
fontsize=12,
color='coral',
fontstyle='italic')
ax2.set_ylabel('Y-axis', fontsize=10, color='gray',
rotation=0, labelpad=20) # Labels not rotated
plt.show()
import numpy as np
x = np.linspace(0, 10, 100)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# Left plot: left-aligned title
ax1.plot(x, np.sin(x), 'steelblue')
ax1.set_title('Left-aligned Title',
loc='left', # Left-aligned
fontsize=12,
color='steelblue')
ax1.set_xlabel('X-axis', fontsize=10, color='gray')
# Right plot: right-aligned title
ax2.plot(x, np.cos(x), 'coral')
ax2.set_title('Right-aligned Title',
loc='right', # Right-aligned
fontsize=12,
color='coral',
fontstyle='italic')
ax2.set_ylabel('Y-axis', fontsize=10, color='gray',
rotation=0, labelpad=20) # Labels not rotated
plt.show()
Example 4: LaTeX Mathematical Formula Title
Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 200)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# Gaussian distribution
sigma, mu = 1.0, 0.0
y1 = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2/(2*sigma**2))
ax1.plot(x, y1, 'steelblue', linewidth=2)
# Use LaTeX formula as title
ax1.set_title(
r'$f(x) = frac{1}{sigmasqrt{2pi}}'
r'expleft(-frac{(x-mu)^2}{2sigma^2}right)$',
fontsize=12)
# Polynomial
y2 = x**3 - 3*x
ax2.plot(x, y2, 'coral', linewidth=2)
ax2.set_title(r'$f(x) = x^3 - 3x$', fontsize=14)
ax2.axhline(y=0, color='gray', linewidth=0.5)
for ax in [ax1, ax2]:
ax.set_xlabel('x')
ax.grid(True, alpha=0.3)
fig.suptitle('Functions with LaTeX Titles', fontsize=14,
fontweight='bold')
plt.show()
print("example: LaTeX titles displayed")
import numpy as np
x = np.linspace(-5, 5, 200)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4),
layout='constrained')
# Gaussian distribution
sigma, mu = 1.0, 0.0
y1 = 1/(sigma * np.sqrt(2*np.pi)) * np.exp(-(x-mu)**2/(2*sigma**2))
ax1.plot(x, y1, 'steelblue', linewidth=2)
# Use LaTeX formula as title
ax1.set_title(
r'$f(x) = frac{1}{sigmasqrt{2pi}}'
r'expleft(-frac{(x-mu)^2}{2sigma^2}right)$',
fontsize=12)
# Polynomial
y2 = x**3 - 3*x
ax2.plot(x, y2, 'coral', linewidth=2)
ax2.set_title(r'$f(x) = x^3 - 3x$', fontsize=14)
ax2.axhline(y=0, color='gray', linewidth=0.5)
for ax in [ax1, ax2]:
ax.set_xlabel('x')
ax.grid(True, alpha=0.3)
fig.suptitle('Functions with LaTeX Titles', fontsize=14,
fontweight='bold')
plt.show()
print("example: LaTeX titles displayed")
FAQ
Difference between title() and suptitle()?
title()(orax.set_title()): Set the title above a single Axes.
suptitle()(orfig.suptitle()): Set the overall title at the top of the entire Figure, covering all subplots.
How to Make the Title Support Chinese?
Set the font:plt.rcParams['font.sans-serif'] = ['SimHei']orplt.rcParams['font.family'] = 'sans-serif'。
Other Extensions