Using pyplot to create grids of plots
<p>I am new to python and having some difficulties with plotting using <code>pyplot</code>. My goal is to plot a grid of plots in-line (<code>%pylab inline</code>) in Juypter Notebook.</p>

<p>I programmed a function <code>plot_CV</code> which plots cross-validation erorr over the degree of polynomial of some x where across plots the degree of penalization (lambda) is supposed to vary. Ultimately there are 10 elements in lambda and they are controlled by the first argument in <code>plot_CV</code>. So </p>

<pre><code>fig = plt.figure()
ax1 = fig.add_subplot(1,1,1) 
ax1 = plot_CV(1,CV_ve=CV_ve)
</code></pre>

<p>Gives</p>

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

<p>Now I think I have to use <code>add_subplot</code> to create a grid of plots as in</p>

<pre><code>fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1 = plot_CV(1,CV_ve=CV_ve)
ax2 = fig.add_subplot(2,2,2)
ax2 = plot_CV(2,CV_ve=CV_ve)
ax3 = fig.add_subplot(2,2,3)
ax3 = plot_CV(3,CV_ve=CV_ve)
ax4 = fig.add_subplot(2,2,4)
ax4 = plot_CV(4,CV_ve=CV_ve)
plt.show()
</code></pre>

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

<p>If I continue this, however, then the plots get smaller and smaller and start to overlap on the x and y labels. Here a picture with a 3 by 3 plot.</p>

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

<p>Is there a way to space the plots evenly, so that they do not overlap and make better use of the horizontal and vertical in-line space in Jupyter Notebook? To illustrate this point here a screenshot from jupyter:</p>

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

<p>Final note: I still need to add a title or annotation with the current level of lambda used in <code>plot_CV</code>.</p>

<hr>

<p><strong>Edit:</strong> Using the tight layout as suggested, gives:</p>

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

<hr>

<p><strong>Edit 2</strong>: Using the <code>fig.set_figheight</code> and <code>fig.set_figwidth</code> I could finally use the full length and heigth available.</p>

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

<p>A first suggestion to you problem would be taking a look at the "<a href="http://matplotlib.org/users/tight_layout_guide.html" rel="nofollow">Tight Layout guide</a>" for matplotlib. </p>

<p>They have an example that looks visually very similar to your situation. As well they have examples and suggestions for taking into consideration axis labels and plot titles.</p>

<p>Further more you can control the over all figure size by using Figure from the <a href="http://matplotlib.org/api/figure_api.html" rel="nofollow">matplotlib.figure</a> class. </p>

<p>Figure(figsize = (x,y))</p>

<p>figsize: x,y (inches)</p>

<p><strong>EDIT:</strong> </p>

<p>Here is an example that I pulled from the matplotlib website and added in the: </p>

<p>fig.set_figheight(15)
fig.set_figwidth(15)</p>

<pre><code>import matplotlib.pyplot as plt

plt.rcParams['savefig.facecolor'] = "0.8"

def example_plot(ax, fontsize=12):
     ax.plot([1, 2])
     ax.locator_params(nbins=3)
     ax.set_xlabel('x-label', fontsize=fontsize)
     ax.set_ylabel('y-label', fontsize=fontsize)
     ax.set_title('Title', fontsize=fontsize)

plt.close('all')
fig = plt.figure()

fig.set_figheight(15)
fig.set_figwidth(15)


ax1 = plt.subplot2grid((3, 3), (0, 0))
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)

example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)

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

<p>You can achieve padding of your subplots by using tight_layout this way:</p>

<p>plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)</p>

<p>That way you can keep your subplots from crowding each other even further.</p>

<p>Have a good one!</p>

python matplotlib plot