Save a plot as an image in Matplotlib

Python

Matplotlib

Figure

Save as image

Luc B.

Python

Matplotlib

Matplotlib visualizations would hardly be useful if they could only be viewed by running Python code--many people would be barred from looking at plots if this was the case! Fortunately, Matplotlib includes an API for saving plots in many popular image formats (find the list of supported image formats here).

Code Example

Use the plt.savefig() function to save the figure as an image. The image file type is inferred from the extension.

import matplotlib.pyplot as plt

# Save the plot as a 'png' file
plt.savefig('my_plot.png')

# Save the plot as a 'pdf' file
plt.savefig('my_plot.pdf')