Okay, let's translate the concepts of Figure and Axes into a practical Python script. While you can create plots interactively (especially in environments like Jupyter notebooks), understanding the structure of a typical plotting script is fundamental for creating reusable and more complex visualizations.
Most Matplotlib plotting scripts follow a standard sequence of steps:
pyplot
module.plt.subplots()
.plot()
, scatter()
, bar()
, etc.) on the Axes
object.Axes
object.plt.show()
to display the figure.Let's look at a minimal script that puts these steps together to create a simple line plot.
# 1. Import necessary libraries
import matplotlib.pyplot as plt
# 2. Prepare your data
x_values = [0, 1, 2, 3, 4, 5]
y_values = [0, 1, 4, 9, 16, 25] # Simple y = x^2 data
# 3. Create the Figure and Axes
fig, ax = plt.subplots() # Creates a Figure and a single Axes object
# 4. Plot the data on the Axes
ax.plot(x_values, y_values)
# 5. Customize the plot (Optional but recommended)
ax.set_title("Simple Plot: y = x^2")
ax.set_xlabel("X Values")
ax.set_ylabel("Y Values (Squared)")
# 6. Show the plot
plt.show()
Let's break down the key parts:
import matplotlib.pyplot as plt
: This imports the pyplot
module, which provides a convenient interface to the Matplotlib library. We use the standard alias plt
.x_values = ...
, y_values = ...
: Here, we're just defining simple Python lists containing our data points. In real-world scenarios, this data might come from files, databases, or calculations.fig, ax = plt.subplots()
: This is a very common and important pattern. The plt.subplots()
function creates two objects for us:
fig
: Represents the entire Figure or window. You might use this later for tasks like saving the plot or adjusting overall figure properties.ax
: Represents the Axes, the actual subplot where the plotting happens. Most of the plotting commands you'll use (plot()
, scatter()
, set_title()
, set_xlabel()
, etc.) are methods of this Axes
object. Using ax
makes your code explicit about where you are plotting.ax.plot(x_values, y_values)
: This is the command that actually draws the line plot. We call the plot()
method on the ax
object we created, passing our x and y data.ax.set_title(...)
, ax.set_xlabel(...)
, ax.set_ylabel(...)
: These methods, also called on the ax
object, add descriptive elements to our plot, making it understandable.plt.show()
: This function tells Matplotlib to render and display the figure(s) you've created. In some interactive environments (like Jupyter), plots might appear automatically, but explicitly calling plt.show()
ensures your script displays the plot when run, for example, from a standard Python file. It's good practice to include it.Executing this script will open a window displaying the line plot:
A simple line plot generated by the basic script, showing y=x2 for x from 0 to 5.
This basic structure forms the foundation for creating almost any plot with Matplotlib. While there are alternative ways to generate plots (sometimes you might see code using plt.plot()
directly), using the fig, ax = plt.subplots()
approach and calling methods on the ax
object (the object-oriented style) is generally recommended. It provides better control, especially when you start working with multiple subplots on the same figure, and makes your code clearer about which part of the visualization you are modifying.
In the following sections, we will dive into specific plotting functions like ax.plot()
and ax.scatter()
, and explore the various customization options available through the Axes
object methods.
© 2025 ApX Machine Learning