How to export to PDF a graph based on a Pandas dataframe in Matplotlib?


To export to PDF a graph based on a Pandas dataframe, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Make a Pandas dataframe with three columns, col1, col2 and col3.
  • Plot the dataframe using plot() method.
  • To display the figure, use show() method.

Example

import matplotlib.pyplot as plt
import pandas as pd

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame([[2, 1, 4], [5, 2, 1], [4, 0, 1]], columns=['col1', 'col2', 'col3'])
df.plot()

plt.savefig('pd_df.pdf')

Output

When we execute the code, it will save the following plot in a PDF with the name "pd_df.pdf".

Updated on: 08-Jul-2021

961 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements