Skip to main content

Saving and Exporting Plots in Matplotlib

After creating and customizing your plots in Matplotlib, you often need to save them for use in reports, presentations, or further analysis. Matplotlib makes it easy to save plots in a variety of formats, including PNG, PDF, SVG, and more. In this article, we’ll explore how to save and export your plots effectively.


1. Saving Plots as PNG Files

PNG is a common format for saving plots, as it provides good quality and is widely supported across different platforms.

1.1 Saving a Plot as a PNG File

You can save a plot as a PNG file using the plt.savefig() function.

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 35]

# Creating a simple plot
plt.plot(x, y)
plt.title('Simple Line Plot')

# Saving the plot as a PNG file
plt.savefig('line_plot.png')

1.2 Adjusting the Resolution

You can adjust the resolution of the saved image by specifying the dpi (dots per inch) parameter.

# Saving the plot with higher resolution
plt.savefig('line_plot_high_res.png', dpi=300)

2. Saving Plots as PDF Files

PDF is a vector format that preserves the quality of your plots, making it ideal for print and professional reports.

2.1 Saving a Plot as a PDF File

You can save a plot as a PDF file by simply changing the file extension.

# Saving the plot as a PDF file
plt.savefig('line_plot.pdf')

3. Saving Plots as SVG Files

SVG (Scalable Vector Graphics) is another vector format that allows for high-quality, scalable plots, suitable for web use and further editing in vector graphics software.

3.1 Saving a Plot as an SVG File

Saving a plot as an SVG is as simple as changing the file extension.

# Saving the plot as an SVG file
plt.savefig('line_plot.svg')

4. Customizing the Save Options

Matplotlib allows for additional customization when saving plots, such as controlling the bounding box, padding, and transparency.

4.1 Adjusting the Bounding Box

The bounding box controls the amount of whitespace around the plot. You can reduce whitespace using the bbox_inches parameter.

# Saving the plot with a tight bounding box
plt.savefig('line_plot_tight.png', bbox_inches='tight')

4.2 Adding Padding Around the Plot

You can add padding around the plot using the pad_inches parameter.

# Saving the plot with additional padding
plt.savefig('line_plot_padded.png', bbox_inches='tight', pad_inches=0.5)

4.3 Saving Plots with Transparency

For plots that need to be overlaid on different backgrounds, you can save them with a transparent background.

# Saving the plot with a transparent background
plt.savefig('line_plot_transparent.png', transparent=True)

5. Exporting Plots for Different Environments

Depending on where you’ll use your plot, you might need to save it in different formats or with specific settings.

5.1 Exporting Plots for Web Use

When exporting plots for use on the web, consider using the SVG format for scalability or PNG for raster images.

# Saving a plot for web use
plt.savefig('line_plot_web.svg')

5.2 Exporting Plots for Presentations

For presentations, a high-resolution PNG or a PDF might be most suitable.

# Saving a plot for a presentation
plt.savefig('line_plot_presentation.pdf', dpi=300)

6. Saving Plots with Multiple Figures

If you have multiple figures open and want to save each one, you can do so by specifying the figure object directly.

6.1 Saving Multiple Figures

# Creating two figures
fig1 = plt.figure()
plt.plot(x, y)
plt.title('Figure 1')
plt.savefig('figure1.png')

fig2 = plt.figure()
plt.plot(y, x)
plt.title('Figure 2')
plt.savefig('figure2.png')

6.2 Saving All Open Figures

You can loop through all open figures and save them automatically.

# Looping through and saving all open figures
for i in plt.get_fignums():
plt.figure(i)
plt.savefig(f'figure_{i}.png')

7. Conclusion

Saving and exporting plots in Matplotlib is straightforward, with a variety of formats and options to suit different needs. Whether you need high-resolution images for print, scalable graphics for the web, or transparency for overlays, Matplotlib provides the tools to save your visualizations effectively. In the next article, we’ll explore how to create interactive visualizations using Matplotlib.