Philipp der Rautenberg
Philipp der Rautenberg

Reputation: 2262

How can I keep figure size constant with matplotlib although using pyplot.show()?

Look at the following python example:

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['figure.figsize'] = [8.27, 11.69]
fig = plt.figure()
plt.show() # This line causes problems with figsize
fig.savefig('test.pdf')

If I use "plt.show()" the figure size of test.pdf changes a lot (from

What do I have to do in order to keep it constant?

Upvotes: 3

Views: 6827

Answers (1)

srgerg
srgerg

Reputation: 19329

There's a good demo on adjusting image size in matplotlib figures on the SciPy site.

The effect of show() on the figure size will depend upon which matplotlib backend is used. For example, when I used the TkAgg backend (the default on my system) it added about 12 pixels to the width and height of the figure. But when I switched to the WXAgg backend, the figure size was actually reduced.

In addition, manually resizing the window displayed by show() will also change the figure size. Moreover, if displaying the figure would require a window too large for the screen, then the window size will be reduced, and the figure size reduced accordingly.

In any case, your best bet is likely going to be to reset the figure size before rendering the pdf. I.e.:

fig.set_size_inches(8.27, 11.69)
fig.savefig('test.pdf')

Upvotes: 6

Related Questions