Matplotlib is a cornerstone library for creating static, interactive, and animated visualizations in Python. While other libraries like Seaborn (which we'll cover soon) provide higher-level interfaces for specific statistical plots, understanding Matplotlib's structure is fundamental because many other plotting libraries build upon it. Think of Matplotlib as the engine that drives much of Python's 2D plotting capabilities.
To effectively use Matplotlib, it's helpful to understand its main components. Visualizations are built layer by layer, starting with the overall container and adding specific plot elements.
At the highest level, we have the Figure
. You can imagine this as the entire window or page on which everything is drawn. It's the top-level container for all plot elements, including titles, figure legends, and, most importantly, one or more Axes
.
An Axes
object represents an individual plot or graph (don't confuse Axes
plural with Axis
singular). It's the region where data is plotted with x and y axes (or other coordinates in 3D). A single Figure
can contain multiple Axes
objects, often arranged in a grid. This is useful for comparing different views of your data side-by-side. Each Axes
has its own title, x-label, and y-label.
Within an Axes
, you have the Axis
objects (typically two for 2D plots: x-axis and y-axis). These objects handle the scaling of the data range (limits), the tick marks (locations along the axis), and the tick labels (the text representing the values at the ticks).
Here's a conceptual breakdown:
This diagram shows the hierarchy: a
Figure
contains one or moreAxes
, and eachAxes
containsAxis
objects and the actual plotted data (lines, points, etc.), along with descriptive elements like titles and labels.
pyplot
and Object-OrientedMatplotlib offers two primary ways to create plots:
pyplot
module: This provides a collection of functions (plt.plot()
, plt.title()
, etc.) that implicitly operate on the "current" figure and axes. It's often used for quick, interactive plotting because it requires less code to get started. It maintains an internal state, automatically creating figures and axes when needed.Figure
and Axes
objects and then calling methods directly on these objects (e.g., ax.plot()
, ax.set_title()
). This approach is generally recommended for more complex plots, reusable functions, or when you need finer control over the figure elements. It makes the code clearer about which plot is being modified.While pyplot
is convenient for simple tasks, we will primarily focus on the object-oriented approach in this course, as it aligns better with building more structured and potentially complex visualizations often needed in machine learning workflows.
Let's see how to create a basic plot using the OO interface. The standard first step is to import the necessary libraries:
import matplotlib.pyplot as plt
import numpy as np
We typically import matplotlib.pyplot
as plt
. Even when using the OO interface, pyplot
is used for functions like creating the figure and axes (plt.subplots()
) and displaying the plot (plt.show()
).
Next, we create a Figure
and a set of Axes
. The function plt.subplots()
is a convenient way to do this. It returns a tuple containing the Figure
object and one or more Axes
objects.
# Sample data
x = np.linspace(0, 10, 100) # 100 points from 0 to 10
y = np.sin(x)
# Create a Figure and an Axes object
fig, ax = plt.subplots()
# Plot data on the Axes
ax.plot(x, y, color='#1c7ed6', linewidth=2) # Blue line
# Customize the plot using Axes methods
ax.set_title("Simple Sine Wave")
ax.set_xlabel("X values")
ax.set_ylabel("Y values (sin(x))")
ax.grid(True, linestyle=':', color='#adb5bd') # Add a light gray grid
# Display the plot
plt.show()
This code snippet performs the following actions:
matplotlib.pyplot
and numpy
.plt.subplots()
creates a Figure
(assigned to fig
) and a single Axes
within that figure (assigned to ax
).ax.plot(x, y, ...)
draws the line plot directly onto the Axes
object ax
. We specify the data and optionally customize its appearance (color, line width).ax.set_title()
, ax.set_xlabel()
, and ax.set_ylabel()
add descriptive text to the plot, again by calling methods on the ax
object.ax.grid(True, ...)
adds a background grid for easier reading.plt.show()
displays the figure that contains our plot. In some environments like Jupyter notebooks, plots might render automatically, but explicitly calling plt.show()
is good practice, especially in scripts.This basic structure using fig, ax = plt.subplots()
and then calling methods on ax
(like ax.plot
, ax.set_title
) forms the foundation for creating a wide variety of visualizations with Matplotlib, which we will build upon in the following sections. Understanding the roles of the Figure
and Axes
objects is the first step towards mastering data visualization in Python.
© 2025 ApX Machine Learning