Matplotlib has two ways to make the same plot, and mixing them up is the #1 source of confusion. Knowing both — and preferring one — makes your code predictable.
The pyplot (implicit) interface
plt.plot(...) draws on a "current" Axes that Matplotlib tracks for you. Quick for one-off plots:
plt.plot(x, y)plt.title("Sine")plt.xlabel("x")plt.show()
The object-oriented (explicit) interface
You create the Figure and Axes explicitly and call methods on them. More verbose, but unambiguous and scalable:
fig, ax = plt.subplots()ax.plot(x, y)ax.set_title("Sine")ax.set_xlabel("x")plt.show()
Key takeaways
- 01pyplot (plt.plot, plt.title) draws on an implicit "current" Axes — quick but ambiguous.
- 02The OO interface (ax.plot, ax.set_title) is explicit about the target Axes.
- 03Prefer the OO interface — it scales to multiple subplots and is the recommended style.
- 04Method names shift: plt.title → ax.set_title, plt.xlabel → ax.set_xlabel.
