savefig()

noun

say it: “SAYV-fig” /ˈseɪvˌfɪɡ/

In matplotlib, the method that saves a chart, or a page of charts, as an image file such as a PNG.

What does savefig() do in matplotlib?

savefig() is the matplotlib Figure method that writes the whole Figure, one chart or a page of them, to an image file, such as fig.savefig("scoops.png"). The file's ending picks the format (a PNG for a report, an SVG to stay sharp at any size), and dpi sets how many pixels each inch becomes.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.bar(["Vanilla", "Chocolate", "Mint"], [320, 410, 150])
fig.savefig("scoops.png", dpi=150, bbox_inches="tight")

In a sentence

  • “savefig() wrote the chart to scoops.png for the report.”
  • “Ending the file name in .svg made savefig() save a picture that stays sharp when zoomed in.”

Level: Apprentice · More Python terms at this level

Updated