pythonpandaspdfpages

Errors writing multiple figures to a .pdf


When generating a pdf from the below dataframes and series, pandas is generating two blank figures and inserting them into my .pdf file. Figure 1 and Figure 3 are both blank while Figure 2 and Figure 4 are printing to the .pdf file. I had hoped to write all three figures below (fig1, fig2, and fig3) to the .pdf file.

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from pandas.tools.plotting import bootstrap_plot
from pandas.tools.plotting import scatter_matrix
from pandas import *

range_days = 10
ydate = "2014-09-09"
wdate = "2014-08-31"

c_data = [1,2,3,4,3,2,1,3,4,5]
p_data = [1,2,3,4,3,2,1,3,4,5]
d_data = [1,2,3,4,3,2,1,3,4,5]

d = {"C":Series(c_data, index=date_range(wdate, periods=range_days)),"P": Series(p_data, index=date_range(wdate, periods=range_days)),"D":Series(d_data, index=date_range(wdate, periods=range_days))}
d2 = {"C":d["C"], "P":d["P"]}

ts2 = DataFrame(d2, index=date_range(wdate, periods=range_days))
ts = DataFrame(d, index=date_range(wdate, periods=range_days))

pdf_name = "Metrics_" + str(wdate) + "_to_" + str(ydate)+ ".pdf"
pdf = PdfPages(pdf_name)

#P & C over time
fig1 = plt.figure()
ts2.plot(secondary_y=["P"])
pdf.savefig(fig1)

#Density Plot of C
fig2 = plt.figure()
d["C"].plot(kind="kde")
pdf.savefig(fig2)

#scatter matrix on the data_frame
fig3 = plt.figure()
scatter_matrix(ts, alpha=0.2, figsize=(6, 6), diagonal='kde')
pdf.savefig(fig3)

pdf.close()

Solution

  • Removal of some of the plt.figure() variables removes the blank figures being created and their insertion to the pdf file.

    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_pdf import PdfPages
    from pandas.tools.plotting import bootstrap_plot
    from pandas.tools.plotting import scatter_matrix
    from pandas import *
    
    range_days = 10
    ydate = "2014-09-09"
    wdate = "2014-08-31"
    
    c_data = [1,3,3,2,3,2,1,6,7,9]
    p_data = [0,5,6,11,3,2,1,1,3,4]
    d_data = [1,7,3,8,3,2,1,3,2,3]
    
    d = {"C":Series(c_data, index=date_range(wdate, periods=range_days)),"P": Series(p_data, index=date_range(wdate, periods=range_days)),"D":Series(d_data, index=date_range(wdate, periods=range_days))}
    d2 = {"C":d["C"], "P":d["P"]}
    
    ts2 = DataFrame(d2, index=date_range(wdate, periods=range_days))
    ts = DataFrame(d, index=date_range(wdate, periods=range_days))
    
    pdf_name = "Metrics_" + str(wdate) + "_to_" + str(ydate)+ ".pdf"
    pdf = PdfPages(pdf_name)
    
    #P & C over time
    ts2.plot(secondary_y=["P"])
    pdf.savefig()
    
    #Density Plot of Conversion
    plt.figure()
    d["C"].plot(kind="kde")
    pdf.savefig()
    
    #scatter matrix on the data_frame
    scatter_matrix(ts, alpha=0.2, figsize=(6, 6), diagonal='kde')
    pdf.savefig()
    
    pdf.close()