6

I'm plotting a grid of subplots with matplotlib (v 1.4.2) in python (v 2.7.9). I can manually adjust the spacing between the subplots, but I'd like different spacing for just some of the subplots. The final figure I'm hoping for is a grid of 2x5 subplots on the left, a grid of 2x5 subplots on the right, and a space in the middle.

The code I'm using to control the figure layout is below:

figw, figh = 16.5, 15.0 #18.5, 15.0
fig, axes = plt.subplots(ncols=4, nrows=5, sharex=False, 
                         sharey=True, figsize=(figw, figh))

plt.subplots_adjust(hspace=0.0, wspace=0.2, left=1/figw,
                    right=1-2./figw, bottom=1/figh, top=1-2./figh)

When I change wspace I get 4 columns all equally spaced. Is there a way of changing wspace in such a way that it's 0 between columns 0 and 1, x between 1 and 2, and 0 between 2 and 3?

Thanks.

1 Answer 1

4

Yes you can if you use GridSpec as described here in the docs: Adjust GridSpec layout

Edit:

A sample code, modified from example above, of how it should look like:

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

f = plt.figure()

plt.suptitle("Different vertical spacings")

gs1 = GridSpec(5, 2)
gs1.update(left=0.05, right=0.48, wspace=0)
ax1 = plt.subplot(gs1[0,0])
ax2 = plt.subplot(gs1[1, 0])
#Add the other subplots for left hand side

gs2 = GridSpec(5, 2)
gs2.update(left=0.55, right=0.98, wspace=0)
ax11 = plt.subplot(gs2[0,0])
ax12 = plt.subplot(gs2[1,0])
#Add the other subplots for right hand side

plt.show()

Haven't been able to test it so hope it works.

Sign up to request clarification or add additional context in comments.

2 Comments

Please avoid link-only answers as links can change, get broken, or the content linked to be updated outside of SO's control.
Just tried it and it worked perfectly. Thanks for the example, I was having trouble getting to grips with info in the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.