Save multiple matplotlib figures in single PDF file – Python

In this tutorial, we will look into how multiple plots can be saved in a single pdf file. In many cases, we require our output to be in a particular format, which we can easily obtain using the following way in Python.

Firstly we will import the required libraries for performing this task.

import pandas as pd
from matplotlib import pyplot as pplot
import seaborn as sns

After importing the required libraries we will insert the CSV file, here I have provided the link to the CSV file which contains the data set that I want to use for performing this task.

dataframe=pd.read_csv("E:\M.PLAN\Code Speedy\PT-OD-Survey .csv")

After loading the data I have displayed the first n rows of the dataset, which by default will show us the first 5 rows.

dataframe.head()

Here firstly for plotting the data on a graph we need to specify the size of the graph. Here the size of the graph is shown by specifying the length of the x and y-axis. After specifying the size we will plot the subplots. Using the subplot function we will first specify the rows and columns that we need to plot and then the order of the plot. After that, we will be using the savefig function to save the plots in a single pdf. Here we can also specify other file formats using the savefig function. The output displayed here is the pdf we got after saving the plot.

pplot.figure(figsize=(10,7))
pplot.subplot(1,2,1)
sns.countplot('Mode used for Egress',hue='Sex', data=dataframe)
pplot.subplot(1,2,2)
sns.countplot('Mode used for Egress',hue='Income per Month (Rs)', data=dataframe)
pplot.savefig('Practice.pdf')

See the PDF

 

You can also read:

Leave a Reply

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