To create effective visualizations with Matplotlib, it's important to understand the basic structure of a plot. Think of it like building something: you need a foundation and then the main structure on top. In Matplotlib, this structure primarily involves two core components: the Figure
and the Axes
.
Imagine you're an artist. Before you start painting, you need a canvas. In Matplotlib, the Figure
object is this canvas. It's the top-level container for everything related to your visualization. This includes all the plotting areas, titles, legends that might apply to the whole figure, and any other elements drawn on the canvas.
You can think of the Figure
as the overall window or page where your plot(s) will appear. It doesn't contain the actual plotted data itself, but it holds the space where the plotting happens. Typically, you'll start by creating a Figure
. While Matplotlib can often create one for you implicitly, understanding it exists is helpful for more control later, especially when creating multiple plots together.
Key characteristics of the Figure
:
Axes
objects.Figure
.Now, think about the actual picture you want to draw on your canvas. This is where the Axes
object comes in. Despite the name sounding plural, a single Axes
object represents one specific plotting area within a Figure
. This is the region where your data is plotted – where you'll see lines, points, bars, etc.
The Axes
object contains most of the elements you typically associate with a plot:
Important Note: Don't confuse Axes
(ending with 'es') with Axis
(ending with 'is'). An Axes
object contains two (or three for 3D) Axis
objects (the x-axis, y-axis, z-axis). Most of the time, you'll be interacting with the Axes
object to create your plots using functions like plot()
, scatter()
, hist()
, etc.
The relationship is straightforward: a Figure
contains one or more Axes
objects.
Figure
containing a single Axes
. This is what you'll work with most often initially.Figure
can hold multiple Axes
objects, arranged in a grid. This is how you create subplots, allowing you to display several related visualizations together on the same canvas.Understanding this hierarchy – the Figure
as the container and the Axes
as the actual plotting area – is fundamental. It provides the framework for organizing and customizing your visualizations effectively. When you call plotting functions, you are typically calling methods belonging to an Axes
object, telling Matplotlib where to draw the data within the overall Figure
canvas.
A diagram illustrating the hierarchical relationship. The
Figure
acts as the overall container, holding one or moreAxes
objects where the actual data visualization takes place.
Grasping the distinction between the overall Figure
canvas and the specific Axes
plotting area(s) is the first step towards mastering Matplotlib. As you progress, you'll see how manipulating these objects directly gives you fine-grained control over every aspect of your visualizations.
© 2025 ApX Machine Learning