Your First Plot
The shortest useful program in matplotlib is three lines: data, plot, and show. What those lines actually do - creating a Figure, creating an Axes, drawing a Line2D artist, asking a backend to display it - is the anatomy every later chapter builds on.
Three Lines
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]
plt.plot(x, y)
plt.show()
In a script or notebook with the right backend, a window or cell image appears showing the parabola. plt.plot with two array-like arguments draws connected line segments; it accepts lists, tuples, NumPy arrays, or anything sequence-like, with x optional (defaults to the index).
What Actually Happened
Behind the convenience call, matplotlib: (1) ensures a current Figure exists, creating one if needed; (2) ensures a current Axes exists in that Figure, creating a single full-size one; (3) constructs a Line2D artist from your data; (4) appends it to the Axes; (5) auto-scales the axes limits to fit the data; and (6) plt.show() hands the Figure to the backend for rendering. Knowing this explains the default behavior - why a second plt.plot draws on the same chart, and why plt.figure() between calls starts a fresh one.
The Same Plot, Explicit Objects
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y = [v * v for v in x]
fig, ax = plt.subplots() # create the Figure and one Axes explicitly
line = ax.plot(x, y) # returns the list of Line2D artists
ax.set_title("squares")
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()
The OO version is the same figure with no ambiguity: ax is the object you are configuring, and it stays that object no matter how many other figures exist. The rest of the tutorial uses fig, ax for multi-element figures and pyplot shorthand only where it is clearly shorter.
Data Options That Matter Immediately
ax.plot(x, y, label="squares")- attach a legend entry (the legend itself is a later chapter).ax.plot(x, y, marker="o")- draw a point at each data value, not just the connecting line.ax.plot(x, y, linestyle="--")- dashed line; the full marker/line vocabulary is the next chapter.- Passing 3D or multi-column data raises or broadcasts - one (x, y) pair per column set, which is why multiple series need separate calls.
Summary
Your first plot is plt.plot(x, y) plus plt.show(), and under the hood it creates a Figure, an Axes, and a line artist. The same figure written as fig, ax = plt.subplots(); ax.plot(x, y) is the explicit form that everything else in this tutorial uses - learn both, and default to the explicit one.