When should you use Matplotlib Pyplot or the object-oriented API?

Quick Overview

Asks how Matplotlib's state-based Pyplot interface differs from its object-oriented Figure and Axes API, and when to pick each one. Tests whether you can explain hidden plotting state and write maintainable multi-panel and reusable plotting code in Python.

When should you use Matplotlib Pyplot or the object-oriented API?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain the difference between Matplotlib's Pyplot API (state-based) and the Object-Oriented API. When would you choose one over the other, and why is the Object-Oriented style generally preferred for complex plots? ```hint Four panels, one title call Picture a figure with four subplots and a single `plt.title("Revenue")` call. Ask what Matplotlib has to decide before it can carry that call out. ``` ### What a Strong Answer Covers - How Pyplot tracks a "current" figure and axes, and what the Object-Oriented API gives you to hold instead - Where Pyplot is the better fit and where its hidden state causes bugs - Why explicit `Figure` and `Axes` references suit multi-panel figures, reusable plotting functions and embedded apps - A short code example in the Object-Oriented style - Practical pitfalls, such as the different method names and figures that are never closed ### Follow-up Questions - How would you write a plotting helper that works both on its own and inside a larger grid of subplots? - A script that saves 1,000 charts in a loop keeps using more memory. Why, and how do you fix it? - How do seaborn's axes-level and figure-level functions fit with the Object-Oriented style? - When you render charts inside a web server or a GUI, why avoid Pyplot entirely?

Overview: Asks how Matplotlib's state-based Pyplot interface differs from its object-oriented Figure and Axes API, and when to pick each one. Tests whether you can explain hidden plotting state and write maintainable multi-panel and reusable plotting code in Python.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the difference between Matplotlib's Pyplot API (state-based) and the Object-Oriented API. When would you choose one over the other, and why is the Object-Oriented style generally preferred for complex plots?

What a Strong Answer Covers Guidance

  • How Pyplot tracks a "current" figure and axes, and what the Object-Oriented API gives you to hold instead
  • Where Pyplot is the better fit and where its hidden state causes bugs
  • Why explicit Figure and Axes references suit multi-panel figures, reusable plotting functions and embedded apps
  • A short code example in the Object-Oriented style
  • Practical pitfalls, such as the different method names and figures that are never closed

Follow-up Questions Guidance

  • How would you write a plotting helper that works both on its own and inside a larger grid of subplots?
  • A script that saves 1,000 charts in a loop keeps using more memory. Why, and how do you fix it?
  • How do seaborn's axes-level and figure-level functions fit with the Object-Oriented style?
  • When you render charts inside a web server or a GUI, why avoid Pyplot entirely?