plt.close()

noun

say it: “PLOT dot KLOHZ” /ˈplɑt dɑt ˈkloʊz/

A matplotlib function that closes a figure so it no longer takes up memory.

What does plt.close() do in matplotlib?

plt.close() closes a matplotlib Figure, so pyplot stops keeping it in memory. pyplot holds on to every Figure it makes, so a test that draws a chart calls plt.close(fig) once it has checked it, and a long test run doesn't fill up memory.

def test_revenue_chart_title():
    fig, ax = revenue_chart(three_sales())
    assert ax.get_title() == "City Park earns the most"
    plt.close(fig)

In a sentence

  • “Each chart test ends with plt.close().”
  • “Without plt.close(), the test run warned about too many open figures.”

Level: Journeyman · More Python terms at this level

Updated