P Keil
P Keil

Reputation: 101

Python Matplotlib: Force figures to have same size

I would like to create multiple plots with the same size. I can achieve this, but the figure sizes still vary slightly, although I don't change the figsize and dpi arguments. This depends on where I put my axis (and probably many other things).

import matplotlib.pyplot as plt

x=[1,2,3]
y=[-2,5,6]

#first plot:
fig, ax = plt.subplots(figsize=(5,5))
ax.plot(x,y)
plt.savefig('plot1.pdf', dpi=300,bbox_inches='tight')
plt.close()

#second plot:
fig, ax = plt.subplots(figsize=(5,5))
ax.plot(x,y)
ax.spines['bottom'].set_position('zero') # change axis
plt.savefig('plot2.pdf', dpi=300,bbox_inches='tight')
plt.close()

The resulting figures have different heights. Can I force the figures to have the same size? I'm using Python 3.6.10 and matplotlib version 3.0.3

Upvotes: 1

Views: 3375

Answers (1)

P Keil
P Keil

Reputation: 101

It turns out the bbox_inches='tight argument adjusts the filesize slightly. Therefore disabling it results in consistent figure sizes. This cuts off my axis labels on the side, so I added plt.rcParams.update({'figure.autolayout': True}).

Upvotes: 3

Related Questions