0

I am having an issue trying to plot 6 figures in a 2 column, 3 row fashion. I have the following code:

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

No matter what I try I get an error saying either too many or too few values to unpack. What I have tried:

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)

This works but gives me one column

fig, (ax1, ax2, ax3, ax4, ax5, ax6) = plt.subplots(6, sharex=True)
1
  • The axes are grouped row-by-row, so 3 rows of 2 columns: fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) = plt.subplots(nrows=3, ncols=2, sharex=True) Commented Apr 5, 2021 at 22:17

1 Answer 1

2

The axes return value is an array with dimensions: rows x cols:

>>> fig, axes = plt.subplot(nrows=3, ncols=2)
>>> type(axes)
numpy.ndarray
>>> axes.shape
(3, 2)

So you can either use a single variable and index into it (e.g. axes[0][1]) or specify the correct dimensions/nesting for unpacking:

fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) = plt.subplots(nrows=3, ncols=2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.