Understanding Figure and Axes
The Figure and Axes are the two most important objects in Matplotlib. Understanding them is key to creating any visualization.
The Hierarchy
Matplotlib organizes plots in a hierarchy:
Figure (the overall window/canvas)
โโโ Axes (an individual plot/chart)
โโโ XAxis (the x-axis)
โโโ YAxis (the y-axis)
โโโ Title
โโโ Lines, Bars, etc. (the actual data)
โโโ Legend, Grid, etc.
Loading Python Playground...
Creating Figures and Axes
There are several ways to create figures and axes:
Method 1: plt.subplots() (Recommended)
Loading Python Playground...
Method 2: plt.figure() + add_subplot()
Loading Python Playground...
Figure Properties
The Figure object controls the overall appearance:
Loading Python Playground...
Axes Properties
Each Axes object has many customizable properties:
Loading Python Playground...
Working with Multiple Axes
Grid of Subplots
Loading Python Playground...
Shared Axes
Loading Python Playground...
Adjusting Spacing
Loading Python Playground...
Practice: Create a Multi-Panel Figure
Loading Python Exercise...
Key Takeaways
- Figure is the overall container (like a canvas or window)
- Axes is an individual plot within the figure
- Use
plt.subplots(rows, cols)to create figures with multiple axes - Access axes in a grid using
axes[row, col]indexing - Customize figures with
figsize,facecolor,dpi - Customize axes with
set_title(),set_xlabel(),set_xlim(),set_xticks() - Use
sharex=Trueorsharey=Truefor linked axes - Use
plt.tight_layout()for automatic spacing
In the next lesson, we'll explore the basic plotting commands you'll use most often.
Up next ยท Lesson 7 of 38
Basic Plotting Commands
1 min read
6 of 38 lessons done

