Matplotlib allows you to save figures as image files using the savefig() function. You can save individual figures or multiple figures in a single file, depending on your needs.

How to Save a Figure in Matplotlib

The savefig() function is used to save the current figure as an image file. By default, it saves the figure in PNG format with transparency.

Why Use savefig()?

  • Allows saving plots for use in other programs.
  • Useful when working with vector graphics editors (e.g., Inkscape, Illustrator).
  • Helps maintain copies of figures in the same directory as your Matplotlib script.

Example: Saving a Pie Chart as an Image File

import matplotlib.pyplot as plt

# Data to plot
labels = ['Angola', 'Brazil', 'Portugal', 'Cape Verde']
sizes = [1215, 2130, 2245, 2210]

# Define colors for each segment
colors = ['lightcoral', 'gold', 'yellowgreen', 'lightskyblue']

# Explode the first slice
explode = (0.1, 0, 0, 0)  

# Create pie chart
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)

# Add legend
plt.legend(labels, loc="upper right")
plt.axis('equal')

# Save the figure as a PNG file
plt.savefig('test.png')

# Display the plot
plt.show()

Understanding the Code:

  • The plt.savefig('test.png') command saves the figure as a PNG file.
  • The file format is determined by the file extension (.png, .jpg, .pdf, etc.).

Saving Figures in Different Formats

Matplotlib allows saving figures in various formats, such as PNG, JPG, and PDF. To specify a format, simply change the file extension in savefig().

For example:

plt.savefig('test.jpg')  # Saves the figure as a JPEG file
plt.savefig('test.pdf')  # Saves the figure as a PDF file
plt.savefig('test.svg')  # Saves the figure as an SVG file

Additional savefig() Options

Matplotlib provides several options to customize how figures are saved. The general syntax is:

plt.savefig(filename, dpi=None, format='png', bbox_inches='tight', pad_inches=0.2)

Explanation of Parameters:

  • filename: The name of the file (e.g., 'chart.png').
  • dpi: Dots per inch (higher values result in better quality images).
  • format: Specifies the image format ('png', 'jpg', 'pdf', etc.).
  • bbox_inches: Defines how much whitespace to include around the figure.
  • pad_inches: Adds padding around the figure.

Example: Saving with Additional Options

import matplotlib.pyplot as plt

# Data to plot
labels = ['Angola', 'Brazil', 'Portugal', 'Cape Verde']
sizes = [1215, 2130, 2245, 2210]

# Define colors
colors = ['lightcoral', 'gold', 'yellowgreen', 'lightskyblue']

# Explode the first slice
explode = (0.1, 0, 0, 0)  

# Create pie chart
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140)

# Add legend
plt.legend(labels, loc="upper right")
plt.axis('equal')

# Save figure with specific settings
plt.savefig('test.png', dpi=300, format='png', bbox_inches='tight', pad_inches=0.2)

plt.show()

Conclusion

Using savefig(), you can easily save your Matplotlib figures in different formats with various customization options. Whether you need a simple PNG file or a high-resolution image for a report, savefig() provides flexibility for saving your plots efficiently.