1

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

asked Oct 11, 2017 at 8:44
0

1 Answer 1

2

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

3 x 3 GridSpec with 2 x 1 GridSpecFromSubplotSpec

answered Oct 11, 2017 at 9:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.