0
0
Matplotlibdata~5 mins

Figure-level methods vs axes-level in Matplotlib

Choose your learning style9 modes available
Introduction

Figure-level and axes-level methods help you control different parts of a plot. Figure-level methods manage the whole picture, while axes-level methods focus on smaller parts inside it.

When you want to change the size or title of the entire plot.
When you want to add labels or change colors of a specific chart inside the plot.
When you want to create multiple charts in one figure and control each separately.
When you want to save or show the whole figure at once.
When you want to customize grid lines or ticks on a single chart.
Syntax
Matplotlib
import matplotlib.pyplot as plt

# Figure-level method example
fig = plt.figure(figsize=(8, 6))
fig.suptitle('Main Title for Whole Figure')

# Axes-level method example
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3], [4, 5, 6])
ax.set_title('Title for One Chart')
ax.set_xlabel('X axis label')
ax.set_ylabel('Y axis label')

Figure-level methods affect the entire figure, which can contain one or more charts (axes).

Axes-level methods affect only one chart inside the figure.

Examples
This sets a title for the whole figure.
Matplotlib
fig = plt.figure()
fig.suptitle('Big Title')
This sets a title for just one chart inside the figure.
Matplotlib
ax = fig.add_subplot(1, 1, 1)
ax.plot([1, 2, 3], [3, 2, 1])
ax.set_title('Small Chart Title')
Here, the figure has two charts stacked vertically. The figure has one main title, and each chart has its own title.
Matplotlib
fig, axs = plt.subplots(2, 1)
fig.suptitle('Figure Title')
axs[0].set_title('Top Chart')
axs[1].set_title('Bottom Chart')
Sample Program

This code creates one figure with two charts side by side. The figure has a main title, and each chart has its own smaller title and labels.

Matplotlib
import matplotlib.pyplot as plt

# Create a figure with two charts
fig, axs = plt.subplots(1, 2, figsize=(10, 4))

# Set a title for the whole figure
fig.suptitle('Figure-level Title: Two Charts')

# First chart: simple line plot
axs[0].plot([1, 2, 3], [1, 4, 9])
axs[0].set_title('Axes-level Title: Chart 1')
axs[0].set_xlabel('X axis')
axs[0].set_ylabel('Y axis')

# Second chart: simple bar plot
axs[1].bar([1, 2, 3], [3, 2, 5])
axs[1].set_title('Axes-level Title: Chart 2')
axs[1].set_xlabel('Categories')
axs[1].set_ylabel('Values')

# Show the plot
plt.show()
OutputSuccess
Important Notes

Figure-level methods like fig.suptitle() set properties for the whole figure.

Axes-level methods like ax.set_title() set properties for individual charts.

Using both lets you organize complex plots clearly.

Summary

Figure-level methods control the entire plot area.

Axes-level methods control individual charts inside the plot.

Use figure-level for overall titles and size, axes-level for chart details.