plt.plot talks to the current Axes. That is convenient for one figure. Several calls in a row still draw on the same Axes until you make a new figure.
Goal
See the current Axes, start a second figure, and know when pyplot state gets in the way.
One Axes, several calls
x = np.linspace(0, 2 * np.pi, 80)
plt.plot(x, np.sin(x), label="sin")
plt.plot(x, np.cos(x), label="cos")
print("current Axes:", plt.gca())
plt.legend()
plt.show()plt.gca() is “get current Axes”.
A second figure
x = np.linspace(0, 4, 50)
plt.figure()
plt.plot(x, x)
plt.title("linear")
plt.figure()
plt.plot(x, x ** 2)
plt.title("quadratic")
plt.show()Each plt.figure() starts a new canvas. Both appear after one plt.show().
Why the next chapter exists
x = np.linspace(0, 2 * np.pi, 80)
plt.plot(x, np.sin(x))
plt.title("first")
plt.plot(x, np.cos(x))
plt.title("oops — still the same Axes")
plt.show()The second plot added a line to the same Axes and overwrote the title. fig, ax = plt.subplots() (next chapter) names the Axes you mean.
Tip
Pyplot is fine for a single chart. Name fig and ax as soon as you have two panels or a function that should not touch “whatever is current”.