Skip to main contentSkip to solution

How can you save a matplotlib plot as a file?

Solution

✅ Correct Option: 3

plt.savefig() is the matplotlib function that saves the current figure to a file. The file format comes from the extension, for example plt.savefig("plot.png") or plt.savefig("plot.pdf").

Call it before plt.show(). After show() closes the window, the figure may be cleared and the saved file can come out blank.


Options 1, 2 and 4: export(), save() and store() sound like natural names for saving, but pyplot has no such functions.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question