Saving figures in Matplotlib as PDF in Python

Matplotlib is a Python 2D plotting library that produces figures and plots, and in this article, we will learn how to save them as a PDF. Tutorial: Saving figures in Matplotlib as PDF in Python.

The savefig() function in Python

Matplotlib has a sub-package called ‘pyplot’. This contains all functions that we need to use to generate and save the plot.

First, we need to create a graph for it to be saved in a file.


import matplotlib.pyplot as grph

list1 = [10, 20, 30, 40, 50, 60]
list2 = [39, 27, 14, 60, 8, 49]

grph.plot(list1, list2, color="#ffccdd", linewidth=2)

This creates a graph, albeit without any labels on the axes. To add labels to the axes, we use “grph.xlabel(‘List1’)”



Now, our graph is completed. To view the graph, use “”.

After we see the graph, we can do more changes if necessary, or go on to save the output as a .pdf file. To do this, use “grph.savefig()”. Since we want to save this figure as a pdf file, give it a filename and add .pdf at the end. Please note that this method will show an error if the filename you specify is already taken by another file in the same directory.


This will save the graph as a pdf file in the same location where the source code file is located.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *