N.Quentin N.Quentin - 11 months ago 51
Python Question

How can I save a plot as pdf file but it has to look like the result of plt.show()?

I didn't know how to formulate my question, you will understand a lot better with some screenshots.

I'm currently saving my figure with:

plt.savefig('test.pdf', bbox_inches='tight')

The result file looks like this:

screenshot of my pdf file

Right after I call the
function, I call
and here is what i want in the pdf file :

what i'm looking for

As you can see there's some significant differences between the two pictures, and I don't know why.

Answer Source

That is a size problem, as the savefig instruction is saving it with a non suitable shape. In order to get a proper size, you have to set the figsize parameter to the one that fits your plot:

width = 18
height = 6
plt.figure(figsize=(width, height))
plt.savefig('test.pdf', bbox_inches='tight')