Figure vs Axes in Matplotlib
In this lesson, you will understand the difference between Figure and Axes objects in Matplotlib, explore their responsibilities, and learn how they work together to create and organize data visualizations.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand the concept of a Figure in Matplotlib.
- Understand the concept of an Axes object.
- Identify the difference between Figure and Axes.
- Understand how Figure and Axes work together.
- Create Figure and Axes objects using Matplotlib.
- Understand the role of an Axis within an Axes object.
- Work with multiple Axes inside a single Figure.
- Use the object-oriented Matplotlib approach.
2. Introduction to Figure and Axes
Matplotlib uses a hierarchical structure to create and manage visualizations. Two important components of this structure are Figure and Axes.
A Figure represents the complete drawing area or overall container. An Axes object represents an individual plotting area inside the Figure.
Example: Student Performance Report
Suppose Omkar wants to create a student performance report containing two charts. One chart displays student marks, and another chart displays attendance.
He can create one Figure and place two separate Axes objects inside it. Each Axes object can display an independent chart.
3. What Is a Figure in Matplotlib?
A Figure is the top-level container that holds all the visual elements of a Matplotlib visualization. It represents the complete canvas on which plots and other graphical elements are placed.
A Figure can contain one or multiple Axes objects. It can also include titles, legends, colorbars, and other elements.
Creating a Figure
import matplotlib.pyplot as plt
# Create a Figure
fig = plt.figure()
# Display the Figure
plt.show()
Setting Figure Size
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 5))
plt.show()
The figsize parameter specifies the
Figure width and height in inches.
4. What Is an Axes Object in Matplotlib?
An Axes object represents an individual plotting area inside a Figure. It is where actual data visualization takes place.
An Axes object contains the plotting region, X-axis, Y-axis, title, labels, ticks, and plotted data.
Creating an Axes Object
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 4], [10, 20, 15, 30])
plt.show()
The add_subplot(111) method creates
an Axes object using a one-row, one-column layout.
5. Difference Between Figure and Axes
Figure and Axes are related components, but they have different responsibilities in Matplotlib.
| Figure | Axes |
|---|---|
| Represents the complete visualization container. | Represents an individual plotting area. |
| Can contain multiple Axes objects. | Belongs to a Figure. |
| Controls overall figure size and layout. | Controls plotting, labels, ticks, and data display. |
Created using plt.figure(). |
Created using methods such as fig.add_subplot() or plt.subplots(). |
| Can contain figure-level titles and legends. | Can contain individual axes titles, labels, and legends. |
| One Figure can contain several plots. | One Axes object generally represents one plot area. |
6. Difference Between Axis and Axes
The terms Axis and Axes are different in Matplotlib. An Axes object is the plotting area, while Axis refers to an individual coordinate dimension.
Axes
Axes is the complete plotting area that contains plotted data, coordinate axes, labels, ticks, and other plot components.
Axis
An Axis represents one coordinate dimension, such as the horizontal X-axis or vertical Y-axis.
| Term | Meaning | Example |
|---|---|---|
| Figure | Complete visualization container. | fig |
| Axes | Individual plotting area. | ax |
| Axis | Coordinate dimension. | X-axis or Y-axis |
7. Creating Figure and Axes Together
Matplotlib provides the plt.subplots()
function to create a Figure and one or more Axes
objects together.
Example: Create One Figure and One Axes
import matplotlib.pyplot as plt
# Create Figure and Axes
fig, ax = plt.subplots()
# Plot data
ax.plot([1, 2, 3, 4], [10, 20, 15, 30])
# Add title
ax.set_title("Student Performance")
# Add labels
ax.set_xlabel("Subjects")
ax.set_ylabel("Marks")
plt.show()
The plt.subplots() function returns
two objects: the Figure object and the Axes object.
fig, ax = plt.subplots()
approach is a recommended starting point for learning
Matplotlib's object-oriented interface.
8. Multiple Axes Inside One Figure
A single Figure can contain multiple Axes objects. This is useful when you want to compare different datasets or display related charts together.
Example: Two Subplots
import matplotlib.pyplot as plt
# Create one Figure with two Axes
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
# First Axes
axes[0].plot([1, 2, 3], [10, 20, 15])
axes[0].set_title("Student Marks")
# Second Axes
axes[1].bar(["A", "B", "C"], [70, 85, 90])
axes[1].set_title("Attendance")
# Display the Figure
plt.show()
9. Figure and Axes in the Object-Oriented API
Matplotlib supports an object-oriented approach where developers directly work with Figure and Axes objects.
This approach provides better control over individual plots and is particularly useful for complex visualizations containing multiple subplots.
Common Object-Oriented Methods
| Method | Purpose |
|---|---|
plt.subplots() |
Creates a Figure and Axes objects. |
fig.add_subplot() |
Adds an Axes to an existing Figure. |
ax.plot() |
Creates a line plot on an Axes. |
ax.set_title() |
Sets the title of an Axes. |
ax.set_xlabel() |
Sets the X-axis label. |
ax.set_ylabel() |
Sets the Y-axis label. |
fig.suptitle() |
Sets an overall Figure title. |
fig.savefig() |
Saves the Figure to a file. |
10. Common Mistakes When Using Figure and Axes
1. Confusing Figure with Axes
Figure represents the complete canvas, whereas Axes represents the individual plotting area.
2. Using the Wrong Method
In the object-oriented approach, use Axes methods
such as ax.set_title() rather than
relying on pyplot state for every operation.
3. Incorrect Subplot Indexing
When creating multiple Axes objects, access the correct Axes object using the appropriate index.
4. Forgetting to Display the Figure
When running a standalone Python script, call
plt.show() when you want to display
the generated chart.
fig and ax are common conventions,
but Python does not require these exact names.
11. Classroom Practical Activity
Activity: Create a Figure with Two Axes
Create a Matplotlib application that displays two different charts inside one Figure.
- Import Matplotlib pyplot.
- Create a Figure using
plt.subplots(). - Create two Axes objects side by side.
- Display student marks using a line chart.
- Display student attendance using a bar chart.
- Add individual titles to both Axes.
- Add suitable axis labels.
- Display the complete Figure using
plt.show().
Practice Questions
- What is a Figure in Matplotlib?
- What is an Axes object?
- What is the main difference between Figure and Axes?
- Can a Figure contain multiple Axes objects?
- What is the difference between Axis and Axes?
- Which function creates a Figure and Axes together?
- What is the purpose of
fig.add_subplot()? - How do you set the title of an Axes object?
- What is the object-oriented approach in Matplotlib?
- Why are multiple Axes useful in data visualization?
Summary
In this lesson, you learned about the Figure and Axes components of Matplotlib. A Figure represents the complete visualization container, while an Axes object represents an individual plotting area inside the Figure.
You also explored the difference between Axis and Axes, learned how to create Figure and Axes objects using Matplotlib, and practised creating multiple plots inside one Figure.
Understanding Figure and Axes is an important foundation for creating organized, customizable, and complex Matplotlib visualizations.