5

I have created a pdf that saves several plots created using Matplotlib.

I did the following to create the pdf

from matplotlib.backends.backend_pdf import PdfPages
report = PdfPages('report.pdf')

After creating a plot, I would do this report.savefig() each time. However, I also want to output dataframes I generated into the Pdf. Essentially I want a report contain plots and queried dataframes all in one place. Is it possible to add a dataframe to the Pdf using the one created with PdfPages and if so, how would I do so? If not, is there another approach that would allow the plots and dataframe to be in once place (without having to save individual components and piecing them together)? Would love any suggestions and examples. Thanks!

1 Answer 1

5

Just create a plot of the table, then save that. Given a dataframe such as:

import pandas as pd

df = pd.DataFrame()
df['Animal'] = ['Cow', 'Bear']
df['Weight'] = [250, 450]
df['Favorite'] = ['Grass', 'Honey']
df['Least Favorite'] = ['Meat', 'Leaves']

which looks like:

  Animal  Weight Favorite Least Favorite
0    Cow     250    Grass           Meat
1   Bear     450    Honey         Leaves

you can plot a table version of it like so:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(9,2))
ax = plt.subplot(111)
ax.axis('off')
ax.table(cellText=df.values, colLabels=df.columns, bbox=[0,0,1,1])

Output:

enter image description here

You can style the table plot a little nicer by adding some background color to the cells:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(9,2))
ax=plt.subplot(111)
ax.axis('off')
c = df.shape[1]
ax.table(cellText=np.vstack([df.columns, df.values]), cellColours=[['lightgray']*c] + [['none']*c]*2, bbox=[0,0,1,1])

Output:

enter image description here

See this ongoing thread (from which all these examples were taken) for more ideas/variants.

Edit

It occurred to me that you might want to plot images and tables on the same figure. You can do so to get results like this:

enter image description here

Here's a link to the tutorial that image came from, which has some example code to help get you started.

Sign up to request clarification or add additional context in comments.

7 Comments

What happens when df size is large say 2000
Then you'll have to be careful with your sizes, and possibly break the table out over several pages. You can size a figure to fill a sheet of regular 8.5 x 11 paper (or presumably one standard pdf page) by creating it like so: plt.figure(figsize=(7.5, 10)). That gives you everything but a 1 inch margin (which should be reasonable) to fill up as much as you can.
I guess saving that large data in PDF handle to itself maybe.
Thanks for your help! What does the ax.subplot(111) do? Also, is it possible to make the resolution better? Mine is very blurry.
so how do you include these plots to a pdf file? What is the command? Is it report.savefig(ax.table())
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.