control wspace for matplotlib subplots

ipython, matplotlib, numpy, python

Solution

What you most probably want is `GridSpec`. It gives you the liberty to adjust the `wspace` for subplot groups.

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

fig = plt.figure()
# create a 1-row 3-column container as the left container
gs_left = gridspec.GridSpec(1, 3)

# create a 1-row 1-column grid as the right container
gs_right = gridspec.GridSpec(1, 1)

# add plots to the nested structure
ax1 = fig.add_subplot(gs_left[0,0])
ax2 = fig.add_subplot(gs_left[0,1])
ax3 = fig.add_subplot(gs_left[0,2])

# create a 
ax4 = fig.add_subplot(gs_right[0,0])

# now the plots are on top of each other, we'll have to adjust their edges so that they won't overlap
gs_left.update(right=0.65)
gs_right.update(left=0.7)

# also, we want to get rid of the horizontal spacing in the left gridspec
gs_left.update(wspace=0)

Now we get:

Of course, you'll want to do something with the labels, etc., but now you have the adjustable spacing.

`GridSpec` can be used to produce some quite complicated layouts. Have a look at:

http://matplotlib.org/users/gridspec.html

Problem

I was wondering: I have a `1 row, 4 column` plot. However, the first three subplots share the same `yaxes` extent (i.e. they have the same range and represent the same thing). The forth does not. What I want to do is change the `wspace` of the three first plots so they are touching (and are grouped), and then the forth plot is space out a bit with no overlap of yaxis label, etc. I could do this so simply with a little `photoshop` editing...but I would like to have a coded version. How could I do this?

Original source