Skip to main content
Albero Academy

The Two Interfaces

pyplot (implicit) vs object-oriented (explicit) — and which to use.

Foundations9 min

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:

pyplot style
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:

OO style — recommended
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title("Sine")
ax.set_xlabel("x")
plt.show()

Key takeaways

  1. 01pyplot (plt.plot, plt.title) draws on an implicit "current" Axes — quick but ambiguous.
  2. 02The OO interface (ax.plot, ax.set_title) is explicit about the target Axes.
  3. 03Prefer the OO interface — it scales to multiple subplots and is the recommended style.
  4. 04Method names shift: plt.title → ax.set_title, plt.xlabel → ax.set_xlabel.

Finished this chapter?

Mark it complete to track your progress through Matplotlib.