Matplotlib - Saving Figures



Saving figures in Matplotlib library allows us to export our plots to various file formats such as PNG, PDF, SVG and so on to use those saved plots in various reports, presentations or publications. Matplotlib library provides the savefig() function for to save the plot that we have created.

Common File Formats for Saving

  • PNG (.png) − Good for general-purpose images which supports transparency.

  • JPEG (.jpg) − Suitable for images with smooth gradients but may lose some quality due to compression.

  • PDF (.pdf) − Ideal for vector-based images scalable without loss of quality.

  • SVG (.svg) − Scalable Vector Graphics which suitable for web-based or vector-based graphics.

Saving figures in Matplotlib library is useful for preserving visualizations in various formats by ensuring they can be shared, used or embedded in different contexts as needed. Adjusting the file format and resolution allows us to balance image quality and file size based on your requirements.

Syntax

The following is the syntax and parameters for using the savefig() method.

plt.savefig(fname, dpi=None, bbox_inches='tight', pad_inches=0.1, format=None, kwargs)

Where,

  • fname − The file name or path of the file to save the figure. The file extension determines the file format such as ".png", ".pdf".

  • dpi − Dots per inch i.e. resolution for the saved figure. Default is "None" which uses the Matplotlib default.

  • bbox_inches − Specifies which part of the figure to save. Options include 'tight', 'standard' or a specified bounding box in inches.

  • pad_inches − Padding around the figure when bbox_inches='tight'.

  • format − Explicitly specify the file format. If 'None' the format is inferred from the file extension in fname.

  • kwargs − Additional keyword arguments specific to the chosen file format.

Saving the plot in specified location

In this example we are creating a simple line plot by using the plot() function and then we are trying to save the plotted image in the specified location with the specified filename.

Example

import matplotlib.pyplot as plt
# Data
x = [22,1,7,2,21,11,14,5]
y = [24,2,12,5,5,5,9,12]
plt.plot(x,y)

# Customize the plot (optional)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')

# Display the plot
plt.savefig('matplotlib/Savefig/lineplot.png')
plt.show()
Output

On executing the above code we will get the following output −

Line Plot

Saving plot in .svg format

Here, this is another example of saving the plotted plot by using the savefig() by specifying the file format as svg and dpi as 300 to set the resolution.

Example

import matplotlib.pyplot as plt
# Data
x = [22,1,7,2,21,11,14,5]
y = [24,2,12,5,5,5,9,12]
plt.plot(x,y)

# Customize the plot (optional)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line Plot')

# Display the plot
plt.savefig('matplotlib/Savefig/lineplot2.svg',dpi = 500)
plt.show()
Output

On executing the above code we will get the following output −

SVG Line Plot

Note

  • We should call savefig() before calling show() if we want to save the figure with the exact appearance shown on the screen otherwise empty file will be saved.

  • The file extension in the fname parameter determines the format of the saved file. Matplotlib automatically infers the format if format is None.

Advertisements