How to Export Matplotlib Charts to a PDF

In this post, I’ll show you how to export matplotlib charts to a PDF file.

More specifically, I’ll use a simple example to demonstrate how to export scatter and line charts into the PDF file.

For this exercise, you’ll need to use the following modules in Python:

  • matplotlib
  • pandas

Let’s start with the data-sets that will be used to create the matplotlib charts.

Data set used for the Scatter Chart

Let’s say that you want to display the relationship between two variable:

  • Unemployment_Rate
  • Stock_Index_Price

You may then use a scatter diagram to depict such a relationship.

Here is the data-set that you can use:

 

Unemployment_RateStock_Index_Price
6.11500
5.81520
5.71525
5.71523
5.81515
5.61540
5.51545
5.31560
5.21555
5.21565

 

You can then capture that data in Python by using pandas dataframe as follows:

 

from pandas import DataFrame

Data1 = {'Unemployment_Rate': [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
        }
   
df1 = DataFrame(Data1,columns=['Unemployment_Rate','Stock_Index_Price'])
print (df1)

 

Let’s now review the second data set.

Data set used for the Line Chart

Similarly,  you may want to capture the trend overtime for the following variables:

  • Year
  • Unemployment_Rate

Line charts are useful for such situations. Here is the data that you may use:

 

YearUnemployment_Rate
19209.8
193012
19408
19507.2
19606.9
19707
19806.5
19906.2
20005.5
20106.3

 

You can then apply this code to create the second DataFrame:

 

from pandas import DataFrame

Data2 = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],
        'Unemployment_Rate': [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
       }
   
df2 = DataFrame(Data2,columns=['Year','Unemployment_Rate'])
print (df2)

Create the Scatter Chart

To create the scatter chart you’ll need to use the matplotlib module in Python.

The first DataFrame will be used to create the scatter chart:

 

from pandas import DataFrame
import matplotlib.pyplot as plt
   
Data1 = {'Unemployment_Rate': [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
       }
  
df1 = DataFrame(Data1,columns=['Unemployment_Rate','Stock_Index_Price'])
  
plt.scatter(df1['Unemployment_Rate'], df1['Stock_Index_Price'], color='green')
plt.title('Unemployment Rate Vs Stock Index Price', fontsize=14)
plt.xlabel('Unemployment Rate', fontsize=14)
plt.ylabel('Stock Index Price', fontsize=14)
plt.grid(True)
plt.show()

 

You may change the labels of the title and/or the axies within the above code.

Create the Line Chart

Next, you’ll need to use the second DataFrame to create the line chart:

 

from pandas import DataFrame
import matplotlib.pyplot as plt
   
Data2 = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],
        'Unemployment_Rate': [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
       }
  
df2 = DataFrame(Data2,columns=['Year','Unemployment_Rate'])
  
plt.plot(df2['Year'], df2['Unemployment_Rate'], color='red', marker='o')
plt.title('Unemployment Rate Vs Year', fontsize=14)
plt.xlabel('Year', fontsize=14)
plt.ylabel('Unemployment Rate', fontsize=14)
plt.grid(True)
plt.show()

 

In the final section, we’ll see how to put together all the needed elements in order to export matplotlib charts to a PDF.

Export Matplotlib Charts to a PDF

To export the matplotlib charts to a PDF, you’ll need to import the matplotlib module as follows:

 

from matplotlib.backends.backend_pdf import PdfPages

 

You would also need to specify the path where you’d like to export the PDF file. In my case, I chose to export the PDF file to my Desktop, using the following path:

 

'C:\Users\Doron E\Desktop\Charts.pdf'

 

You’ll need to modify that path to the location where you’d like to store the PDF file on your computer (in the Python code below, look for the line that starts with: ‘with PdfPages‘ to modify the path).

Putting all the components together, you’ll be able to generate the PDF file with the matplotlib charts:

 

from pandas import DataFrame
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages


Data1 = {'Unemployment_Rate': [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
       }   
df1 = DataFrame(Data1,columns=['Unemployment_Rate','Stock_Index_Price'])


Data2 = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],
        'Unemployment_Rate': [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
       }   
df2 = DataFrame(Data2,columns=['Year','Unemployment_Rate'])


with PdfPages(r'C:\Users\Doron E\Desktop\Charts.pdf') as export_pdf:
  
    plt.scatter(df1['Unemployment_Rate'], df1['Stock_Index_Price'], color='green')
    plt.title('Unemployment Rate Vs Stock Index Price', fontsize=10)
    plt.xlabel('Unemployment Rate', fontsize=8)
    plt.ylabel('Stock Index Price', fontsize=8)
    plt.grid(True)
    export_pdf.savefig()
    plt.close()
      
    plt.plot(df2['Year'], df2['Unemployment_Rate'], color='red', marker='o')
    plt.title('Unemployment Rate Vs Year', fontsize=10)
    plt.xlabel('Year', fontsize=8)
    plt.ylabel('Unemployment Rate', fontsize=8)
    plt.grid(True)
    export_pdf.savefig()
    plt.close()

 

Once you run the above code, a new PDF file (called Charts) will be created at your specified location.

Once you open that file, you’ll see two pages, where each page would contain a chart.

The first page would contain the scatter chart:

 

How to Export Matplotlib Charts to a PDF

 

While the second page would contain the line chart:

 

Export Matplotlib Charts to a PDF

 

You may specify a different file name by modifying the file name in the highlighted portion (highlighted in green). In our example, we used the file name of ‘Charts’:

with PdfPages(r’C:\Users\Doron E\Desktop\Charts.pdf‘) as export_pdf:

Don’t forget to put the ‘.pdf’ file extension (highlighted in blue) at the end of the path name.

Bonus: Create a button on a tkinter GUI to export the PDF file

You may also create a simple tkinter GUI that will allow users to export the PDF file using a single click.

As indicated before, you’ll need to change the path name (in the code below) to the location where you’d like to store the PDF on your computer.

Here is the full Python code:

 

from pandas import DataFrame
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import tkinter as tk


Data1 = {'Unemployment_Rate': [6.1,5.8,5.7,5.7,5.8,5.6,5.5,5.3,5.2,5.2],
        'Stock_Index_Price': [1500,1520,1525,1523,1515,1540,1545,1560,1555,1565]
       }   
df1 = DataFrame(Data1,columns=['Unemployment_Rate','Stock_Index_Price'])


Data2 = {'Year': [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010],
        'Unemployment_Rate': [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]
       }   
df2 = DataFrame(Data2,columns=['Year','Unemployment_Rate'])


root= tk.Tk()
 
canvas1 = tk.Canvas(root, width = 700, height = 500)
canvas1.pack()


def export():

    with PdfPages(r'C:\Users\Doron E\Desktop\Charts.pdf') as export_pdf:
      
        plt.scatter(df1['Unemployment_Rate'], df1['Stock_Index_Price'], color='green')
        plt.title('Unemployment Rate Vs Stock Index Price', fontsize=10)
        plt.xlabel('Unemployment Rate', fontsize=8)
        plt.ylabel('Stock Index Price', fontsize=8)
        plt.grid(True)
        export_pdf.savefig()
        plt.close()
          
        plt.plot(df2['Year'], df2['Unemployment_Rate'], color='red', marker='o')
        plt.title('Unemployment Rate Vs Year', fontsize=10)
        plt.xlabel('Year', fontsize=8)
        plt.ylabel('Unemployment Rate', fontsize=8)
        plt.grid(True)
        export_pdf.savefig()
        plt.close()
    
button1 = tk.Button (root, text='Export PDF',command=export, bg='brown', fg='white')
canvas1.create_window(360, 250, window=button1)
    
    
root.mainloop()

 

Once you run the above Python code, the following GUI would be displayed:

 

GUI PDF

 

Press the ‘Export PDF’ button, and your PDF would be created at your specified location.

Conclusion

We just saw how to export matplotlib charts to a PDF file.

This process would work for any matplotlib chart that you may want to export to a PDF.

You may also want to check the following source to learn more about creating matplotlib charts in Python.