Python panda PdfPages export to single PDF from different itteration

308 views Asked by At

I'm planning to make a report consist of a lot (total 200+) of seaborn chart and some tables. There are different chart and to build them initially I'm using different script and iteration of PdfPages. But somehow I'm required to compile all the chart into single pdf file output. Both chart type are correlation build using lmplot

enter image description here 1nd type of chart

plottop10 = sns.lmplot(x="xvalue", y="yvalue", col="parameter1",data=plotcorr10,col_wrap=3, sharex=False, sharey=False)

This code return 10 plots

And there is also a table with the r^2 value of those 10 plots

2st type of chart

plotall = sns.lmplot(x="x2value, y="y2value", col="parameter2", data=plotcorrall,col_wrap=3, sharex=False, sharey=False)

This code return 200+ plots

I want to join 1st type of chart with 2nd type of chart. I tried to use PdfPages to get 1st type of chart on the 1st page with the table and 2nd page onwards with 2nd type of chart with each page consists of 15 charts.

Edit: add the code

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

with PdfPages('et.pdf') as pdf:
    #This part to make the table as image, with this I hope can insert inside pdf
    def render_mpl_table(data, col_width=8.0, row_height=0.625, font_size=12,
                         header_color='#40466e', row_colors=['#f1f1f2', 'w'], edge_color='w',
                         bbox=[0, 0, 1, 1], header_columns=0,
                         ax=None, **kwargs):
        if ax is None:
            size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
            fig, ax = plt.subplots(figsize=size)
            ax.axis('off')
        mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, **kwargs)
        mpl_table.auto_set_font_size(False)
        mpl_table.set_fontsize(font_size)

        for k, cell in mpl_table._cells.items():
            cell.set_edgecolor(edge_color)
            if k[0] == 0 or k[1] < header_columns:
                cell.set_text_props(weight='bold', color='w')
                cell.set_facecolor(header_color)
            else:
                cell.set_facecolor(row_colors[k[0]%len(row_colors) ])
        return ax.get_figure(), ax
    fig,ax = render_mpl_table(corrfin, header_columns=0, col_width=7.5)
    plottop10 = sns.lmplot(x="xvalue", y="yvalue", col="parameter",data=plotcorr10,col_wrap=3, sharex=False, sharey=False)
    plt.tight_layout()
    pdf.savefig()
    pdf.close()
    plotall = sns.lmplot(x="x2value", y="y2value", col="parameter", data=plotcorrall,col_wrap=3, sharex=False, sharey=False)
    pdf.savefig()
    pdf.close()

It returns error messages

AttributeError: 'NoneType' object has no attribute 'newPage'
AttributeError: 'NoneType' object has no attribute 'endStream'
AttributeError: 'NoneType' object has no attribute 'finalize'

Reference:

  1. How to save the Pandas dataframe/series data as a figure?
  2. Save the plots into a PDF
0

There are 0 answers