在Python matplotlib中绘制子绘图

在Python matplotlib中绘制子绘图,python,matplotlib,Python,Matplotlib,我有一个问题,试图以2列3行的方式绘制6个数字。我有以下代码: fig, (ax1, ax2, ax3, ax4, ax5, ax6) = plt.subplots(nrows=3, ncols=2, sharex=True) ax1.plot(df_truth.index, df_truth[stat], label = 'Fine') ax1.plot(df_truth.index, df_Obs[stat], label = 'Observation') ax1.plot(df_trut

我有一个问题,试图以2列3行的方式绘制6个数字。我有以下代码:

fig, (ax1, ax2, ax3, ax4, ax5, ax6) = plt.subplots(nrows=3, ncols=2, sharex=True)

ax1.plot(df_truth.index, df_truth[stat], label = 'Fine')
ax1.plot(df_truth.index, df_Obs[stat], label = 'Observation')
ax1.plot(df_truth.index, df_T12_0hr[stat], label = 'T12_0hr')

ax2.plot(df_truth.index, df_truth[stat], label = 'Fine')
ax2.plot(df_truth.index, df_Obs[stat], label = 'Observation')
ax2.plot(df_truth.index, df_T12_12hr[stat], label = 'T12_12hr')

ax3.plot(df_truth.index, df_truth[stat], label = 'Fine')
ax3.plot(df_truth.index, df_Obs[stat], label = 'Observation')
ax3.plot(df_truth.index, df_T12_24hr[stat], label = 'T12_24hr')

ax4.plot(df_truth.index, df_truth[stat], label = 'Fine')
ax4.plot(df_truth.index, df_Obs[stat], label = 'Observation')
ax4.plot(df_truth.index, df_T3_0hr[stat], label = 'T3_0hr')

ax5.plot(df_truth.index, df_truth[stat], label = 'Fine')
ax5.plot(df_truth.index, df_Obs[stat], label = 'Observation')
ax5.plot(df_truth.index, df_T12_0hr[stat], label = 'T3_12hr')

ax6.plot(df_truth.index, df_truth[stat], label = 'Fine')
ax6.plot(df_truth.index, df_Obs[stat], label = 'Observation')
ax6.plot(df_truth.index, df_T3_24hr[stat], label = 'T3_24hr')
 
plt.savefig(figDir+stat+'_watlev_ts.png', bbox_inches = 'tight', pad_inches = 0.02)
plt.close()
print('Plotting ' + str(stat) )
无论我尝试什么,我都会得到一个错误,即值太多或太少,无法解包。 我所尝试的:

fig, (ax1, ax2, ax3, ax4, ax5, ax6) = plt.subplots(nrows=3, ncols=2, sharex=True)
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(3, 2, sharex=True)
fig, ax1, ax2, ax3, ax4, ax5, ax6 = plt.subplots(3,2, sharex=True)
fig, (ax1, ax2, ax3, ax4, ax5, ax6) = plt.subplots(3,2, sharex=True)
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(6, sharex=True)
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(3, 2)
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(3, 2, sharex=True)
fig, ax1, ax2, ax3, ax4, ax5, ax6 = plt.subplots(nrows=3, ncols=2, sharex=True)
这行得通,但给了我一个专栏

fig, (ax1, ax2, ax3, ax4, ax5, ax6) = plt.subplots(6, sharex=True)




     

返回值是一个具有维度的数组:
x
cols

>>> fig, axes = plt.subplot(nrows=3, ncols=2)
>>> type(axes)
numpy.ndarray
>>> axes.shape
(3, 2)
因此,您可以使用单个变量并对其进行索引(例如,
轴[0][1]
),也可以指定解包的正确尺寸/嵌套:

fig,((ax1,ax2),(ax3,ax4),(ax5,ax6))=plt.子批(nrows=3,ncols=2)

轴返回值是一个具有维度的数组:
x
cols

>>> fig, axes = plt.subplot(nrows=3, ncols=2)
>>> type(axes)
numpy.ndarray
>>> axes.shape
(3, 2)
因此,您可以使用单个变量并对其进行索引(例如,
轴[0][1]
),也可以指定解包的正确尺寸/嵌套:

fig,((ax1,ax2),(ax3,ax4),(ax5,ax6))=plt.子批(nrows=3,ncols=2)

轴按行分组,因此3行2列:
fig,((ax1,ax2),(ax3,ax4),(ax5,ax6))=plt.子批(nrows=3,ncols=2,sharex=True)
轴按行分组,因此3行2列:
fig,((ax1,ax2),(ax3,ax4),(ax5,ax6))=plt.子批(nrows=3,ncols=2,sharex=True)