Reputation: 165
I am trying to make a plot of subplots where each subplot comprises out of two subplots sharing the x-axis. I have tried the following code:
gs_top = plt.GridSpec(6, 3, hspace = 0.0001)
gs_base = plt.GridSpec(6, 3, hspace = 0.95)
f2 = plt.figure()
for i in range(9):
up_id = [0,1,2,6,7,8,12,13,15]
bot_id = [3,4,5,9,10,11,15,16,17]
axarr2 = f2.add_subplot(gs_top[up_id[i]])
axarr2.plot()
ax_sub = f2.add_subplot(gs_base[bot_id[i]], sharex= axarr2)
ax_sub.imshow()
axarr2.set_title('title')
axarr2.xaxis.set_visible(False)
How should I set the parameters in plt.GridSpec()
?
Upvotes: 1
Views: 3001
Reputation: 339052
I would guess that you want to use gridspec.GridSpecFromSubplotSpec
to create another gridspec inside a gridspec. So assuming you want a 3 x 3 grid and each cell shall comprise two subplots, vertically attached to each other and sharing their x axes.
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
gs = gridspec.GridSpec(3, 3, hspace=0.6,wspace=0.3)
for i in range(9):
gss = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs[i],
hspace=0.0)
ax0 = fig.add_subplot(gss[0])
ax1 = fig.add_subplot(gss[1], sharex=ax0)
x = np.linspace(0,6*np.pi)
y = np.sin(x)
ax0.plot(x,y)
ax1.plot(x/2,y)
ax0.set_title('title {}'.format(i))
ax0.tick_params(axis="x", labelbottom=0)
plt.show()
Upvotes: 2