How can you save a matplotlib plot as a file?
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.
Related questions:
2026: 21 May Shift 1
2026: 19 May Shift 1
2026: 26 May Shift 1