8

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

I programmed a function plot_CV 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 plot_CV. So

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1) 
ax1 = plot_CV(1,CV_ve=CV_ve)

Gives

enter image description here

Now I think I have to use add_subplot to create a grid of plots as in

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()

enter image description here

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.

enter image description here

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:

enter image description here

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


Edit: Using the tight layout as suggested, gives:

enter image description here


Edit 2: Using the fig.set_figheight and fig.set_figwidth I could finally use the full length and heigth available.

enter image description here

tomka
  • 2,516
  • 7
  • 31
  • 45
  • 1
    Did you try ```plt.tight_layout()``` before ```plt.show()```. Otherwise you would have to decrease the tick-label frequencies and the axis-label sizes. – sascha Sep 23 '16 at 11:56
  • @sascha thanks, see update – tomka Sep 23 '16 at 12:03

1 Answers1

11

The first suggestion to your problem would be taking a look at the "Tight Layout guide" for matplotlib.

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.

Furthermore you can control the overall figure size by using Figure from the matplotlib.figure class.

Figure(figsize = (x,y))

figsize: x,y (inches)

EDIT:

Here is an example that I pulled from the matplotlib website and added in the:

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

example:

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()

You can achieve padding of your subplots by using tight_layout this way:

plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)

That way you can keep your subplots from crowding each other even further.

Have a good one!

Dadep
  • 2,796
  • 5
  • 27
  • 40
Kyle Swanson
  • 1,308
  • 1
  • 12
  • 20
  • Did you achieve your desired plot making design? They look better :) I am curious because I my self will soon be making many plots like this and would like to know. – Kyle Swanson Sep 23 '16 at 12:24
  • No didn't work so far. The results up there under edit still look unsatisfactory to me. It does not react to Figure size control command; no error but also no change in size. – tomka Sep 23 '16 at 12:30
  • Do you want to control all of the subplots sizes individually or the whole plot together? – Kyle Swanson Sep 23 '16 at 12:37
  • It would be sufficient to control the size of the whole plot altogether, so that it becomes more legible. Also, when I make a 3x4 plot, instead of 4x3, the plots get clinched together and do not use the full space to the right of the plot – tomka Sep 23 '16 at 12:43
  • Thank you, the `fig.set_figheight` solved the problem to large extends. I'll paste an update with final results. Perhaps you can improve it further. – tomka Sep 23 '16 at 13:02
  • 1
    Looking better! I am not sure what your next goal is? – Kyle Swanson Sep 23 '16 at 13:10
  • I wouldn't know. Just if you have an idea. I leave it like this for the moment. Thanks again! – tomka Sep 23 '16 at 14:01