Reputation: 2781
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.
Upvotes: 6
Views: 2413
Reputation: 7304
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.
Upvotes: 4