Why might a Matplotlib plot not display when running a script, and what common function call is often missed?

Quick Overview

Asks why a Matplotlib chart that appears in a notebook fails to show up when the same code runs as a Python script, and which call is usually missing. Tests understanding of Matplotlib backends, the GUI event loop, headless environments, and how to save plots when no display exists.

Why might a Matplotlib plot not display when running a script, and what common function call is often missed?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Why might a Matplotlib plot not display when running a script, and what common function call is often missed? The same plotting code shows a chart in a Jupyter notebook, but `python make_plot.py` from a terminal finishes without opening a window. ```hint Compare with a notebook Think about what a notebook does after every cell that a plain script run does not, and what has to be running for a plot window to appear and stay open. ``` ### What a Strong Answer Covers - The missing call and why a script needs it when a notebook does not - What that call does to display the window - Backend causes: a non-interactive backend, a headless environment, a missing GUI toolkit - Other script-level causes, such as figures closed too early or the script exiting before the window can stay open - How to check the active backend, and how to produce plots when no display is available ### Follow-up Questions - How do you check which backend Matplotlib is using, and how do you change it? - A script calls `plt.show()` and then `plt.savefig("out.png")`, and the saved file is blank. Why? - How would you produce plots from a scheduled job on a server with no display? - What does `plt.ion()` change, and why can it make a window flash and disappear when a script ends?

Overview: Asks why a Matplotlib chart that appears in a notebook fails to show up when the same code runs as a Python script, and which call is usually missing. Tests understanding of Matplotlib backends, the GUI event loop, headless environments, and how to save plots when no display exists.

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

Why might a Matplotlib plot not display when running a script, and what common function call is often missed? The same plotting code shows a chart in a Jupyter notebook, but python make_plot.py from a terminal finishes without opening a window.

What a Strong Answer Covers Guidance

  • The missing call and why a script needs it when a notebook does not
  • What that call does to display the window
  • Backend causes: a non-interactive backend, a headless environment, a missing GUI toolkit
  • Other script-level causes, such as figures closed too early or the script exiting before the window can stay open
  • How to check the active backend, and how to produce plots when no display is available

Follow-up Questions Guidance

  • How do you check which backend Matplotlib is using, and how do you change it?
  • A script calls plt.show() and then plt.savefig("out.png") , and the saved file is blank. Why?
  • How would you produce plots from a scheduled job on a server with no display?
  • What does plt.ion() change, and why can it make a window flash and disappear when a script ends?