How does one control whitespace around a figure in matplotlib?(plt.tight_layout does not work)
<p>I have a matplotlib figure with 3 sub-plots. The consensus from stackexchange seems to be to use the plt.tight_layout in order to get rid of the whitespace around the figure. This does not solve the problem in my case.</p>

<p>My code is as follows:</p>

<pre><code>import numpy as np
import os
import pandas as pd
import matplotlib as mpl
from matplotlib import pyplot as plt

my_dpi = 1500
plt.ion()

index = np.linspace(0,5,10)
combined = np.linspace(0,1,10)
case1 = np.linspace(0,1,10)
case2 = np.linspace(0,1,10)
case3 = np.linspace(0,1,10)

tsfont = {'fontname':'Times-Roman'}
mpl.rcParams.update({'font.size': 18})
mpl.rc('font',family='Arial')
ms = 0

f = plt.figure()
f.set_size_inches(7.5,10)
f.axison=False


lw = 2
asp = 5


axarr1 = plt.subplot(3,1,1, adjustable='box',aspect = asp)

axarr1.set_xlim(0,5,10)
axarr1.set_ylim(0,1)
axarr1.set_ylabel('y')
axarr1.set_xlabel('$\\tau$', fontsize =25)
p = axarr1.plot(index,combined,color='navy', linewidth=lw, label = "Healthy")
axarr1.xaxis.set_label_coords(0.5, -0.05)


'''
Duct 2
'''

axarr2 = plt.subplot(3,1,2, adjustable='box',aspect = asp)
#axarr2.set_aspect('auto')
axarr2.set_xlim(0,5,10)
axarr2.set_ylim(0,1)
axarr2.set_ylabel('y')
axarr2.set_xlabel('$\\tau$', fontsize = 25)
g = axarr2.plot(index,combined,color='navy', linewidth=lw)
axarr2.xaxis.set_label_coords(0.5, -0.05)

'''
Duct 3
'''

axarr3 = plt.subplot(3,1,3, adjustable='box',aspect = asp)

axarr3.set_xlim(0,5,10)
axarr3.set_ylim(0,1)
axarr3.set_ylabel('y')
axarr3.set_xlabel('$\\tau$', fontsize = 25)
w = axarr3.plot(index,combined,color='navy', linewidth=lw)
axarr3.xaxis.set_label_coords(0.5, -0.05)

#plt.tight_layout()
plt.show()
</code></pre>

<p>Without using plt.tight_layout() I get the following result</p>

<p><a href="http://i.stack.imgur.com/WgVVo.png" rel="nofollow"><img src="http://i.stack.imgur.com/WgVVo.png" alt="enter image description here"></a></p>

<p>Uncommenting the relevant line gives me</p>

<p><a href="http://i.stack.imgur.com/SfOso.png" rel="nofollow"><img src="http://i.stack.imgur.com/SfOso.png" alt="As is obvious, while the vertical spacing changes, the horizontal spacing does not"></a></p>

<p>As is obvious, while the vertical spacing changes, the horizontal spacing does not</p>

<p>I'd like to know how to get rid of the horizontal whitespacing to the left and right.</p>

<p>The <code>tight_layout</code> option places the images closer to the borders. However, in your case, there is nothing to fill this empty space with, so it doesn't work.</p>

<p>If you want a narrower figure, you should change the horizontal dimension, e.g.:</p>

<pre><code>plt.figure(..., tight_layout=True, figsize=(12, 5))
</code></pre>

<p>This is in inches, experiment with the numbers until it looks good.</p>

<p>You could also use 2 by 2 subplots to keep a square figure and use the space better:</p>

<pre><code>plt.subplot(2, 2, ...)
</code></pre>

<p>although this may look suboptimal with a prime number of figures.</p>

python numpy matplotlib plot