我正在尝试绘制子图,其中每个子图包含两个共享x轴的子图。我尝试了以下代码:
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)
如何在plt.GridSpec()
?
答案 0 :(得分:0)
我猜你想用gridspec.GridSpecFromSubplotSpec
在gridspec中创建另一个gridspec。因此,假设您需要一个3 x 3网格,每个单元格应包含两个子图,彼此垂直相连并共享其x轴。
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()