Why does plt.show() behave differently in a script versus a Jupyter notebook?

Quick Overview

Asks why Matplotlib's plt.show() blocks and opens a window in a Python script but behaves differently inside a Jupyter notebook. Tests understanding of Matplotlib backends, the inline renderer, headless environments, and common figure-saving pitfalls.

Why does plt.show() behave differently in a script versus a Jupyter notebook?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Why does `plt.show()` behave differently in a Python script than in a Jupyter notebook? Explain what Matplotlib is doing in each case and what that means when you write plotting code. ```hint Backends Find out which Matplotlib backend is active in each environment, and what that backend does when it is asked to show a figure. ``` ### What a Strong Answer Covers - What a Matplotlib backend is and which kind is active in a script compared with a notebook. - What `plt.show()` does under a GUI backend, including blocking and the event loop. - What the inline backend does at the end of a cell, and what calling `plt.show()` still changes there. - Practical consequences: headless machines, saving figures, reusing a figure across cells, and plotting in a loop. ### Follow-up Questions - A script runs on a server with no display and `plt.show()` produces nothing. What is happening, and how do you get the chart? - Why does calling `plt.savefig()` after `plt.show()` sometimes write a blank image? - How would you get zoomable, interactive figures inside a notebook? - In a notebook, how do you add a title to a figure that was drawn in an earlier cell?

Overview: Asks why Matplotlib's plt.show() blocks and opens a window in a Python script but behaves differently inside a Jupyter notebook. Tests understanding of Matplotlib backends, the inline renderer, headless environments, and common figure-saving pitfalls.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Why does plt.show() behave differently in a Python script than in a Jupyter notebook? Explain what Matplotlib is doing in each case and what that means when you write plotting code.

What a Strong Answer Covers Guidance

  • What a Matplotlib backend is and which kind is active in a script compared with a notebook.
  • What plt.show() does under a GUI backend, including blocking and the event loop.
  • What the inline backend does at the end of a cell, and what calling plt.show() still changes there.
  • Practical consequences: headless machines, saving figures, reusing a figure across cells, and plotting in a loop.

Follow-up Questions Guidance

  • A script runs on a server with no display and plt.show() produces nothing. What is happening, and how do you get the chart?
  • Why does calling plt.savefig() after plt.show() sometimes write a blank image?
  • How would you get zoomable, interactive figures inside a notebook?
  • In a notebook, how do you add a title to a figure that was drawn in an earlier cell?