Skip to content
tezvyn:

Matplotlib's Object-Oriented API: Explicit Plot Control

Source: matplotlib.orgMediumHow cards are made

Matplotlib's Object-Oriented API: Explicit Plot Control

Instead of the stateful plt.plot(), Matplotlib's OO API gives you explicit control by creating Figure and Axes objects to call methods on, like ax.plot(). This is crucial for complex plots with multiple subplots. The footgun is mixing styles.

Why it exists

The simple plt.plot() interface is great for quick sketches but becomes ambiguous and hard to manage for complex charts. The Object-Oriented (OO) API was designed to provide an explicit, robust, and powerful way to build visualizations by giving you direct control over every component of the plot.

The mental model

Think of a plot as a hierarchy of objects. The Figure is the entire canvas or window. Inside the Figure are one or more Axes objects; each Axes is an individual plot with its own data, coordinate system, ticks, and labels. The OO API gives you direct handles to these Figure and Axes objects, so you can command them explicitly. It's the difference between telling a robot "make a plot" versus directly manipulating its arms to draw exactly what you want.

How it works

You almost always start a plot with fig, ax = plt.subplots(). This single line creates a Figure object (named fig) and an Axes object (named ax). From that point on, you call methods on these specific objects, not on the global plt module. For plotting, you use ax.plot() instead of plt.plot(). To set a title, you use ax.set_title() instead of plt.title(). Every command explicitly targets a specific part of your visualization, eliminating ambiguity.

When to use it

Use the OO API for any code you plan to save, share, or reuse. It is the standard for production scripts, scientific publications, and complex dashboards with multiple subplots. Its explicitness makes code easier to read, debug, and maintain. If your plot has more than one panel, the OO API is practically mandatory.

When not to use it

The stateful pyplot interface (plt.plot()) is fine for temporary, interactive use in a REPL or Jupyter cell where you just need to see a single, simple plot quickly and will throw the code away. For anything more permanent or complex, the small amount of extra setup for the OO API is worth the clarity.

One canonical example

To create a figure with two side-by-side subplots, the OO approach is far clearer. You would write fig, axs = plt.subplots(1, 2). This gives you an array of Axes objects called axs. You can then plot on the first subplot with axs[0].plot(...) and the second with axs[1].plot(...). The alternative pyplot style requires calling plt.subplot() multiple times to switch the "active" plot, which is stateful and harder to follow.

Interview question

When is Matplotlib's Object-Oriented (OO) API most advantageous to use?

  • a.When you want to use Matplotlib's advanced statistical plotting functions, which are only available through the OO API.
  • b.To simplify plot creation by letting Matplotlib automatically manage plot components and state.
  • c.For developing production-ready scripts, scientific publications, or complex dashboards with multiple panels.Correct
  • d.When you need to quickly visualize data in a REPL or Jupyter notebook for temporary analysis.
Why?

The card states the OO API is for "any code you plan to save, share, or reuse," including "production scripts, scientific publications, and complex dashboards with multiple subplots." Option D describes the use case for the simpler, stateful pyplot interface, not the OO API.

Just read this? Test yourself on what you have been reading.

Read the original → matplotlib.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles