The different return types are due to the squeeze
keyword argument to plt.subplots()
which is set to True
by default.
Let's enhance the documentation with the respective unpackings:
squeeze : bool, optional, default: True
Alternatively you may always use the unpacked version
fig, ax_arr = plt.subplots(nrows=N, ncols=M, squeeze=False)
and index the array to obtain the axes, ax_arr[1,2].plot(..)
.
So for a 2 x 3 grid it wouldn't actually matter if you set squeeze
to False
. The result will always be a 2D array. You may unpack it as
fig, ((ax1, ax2, ax3),(ax4, ax5, ax6)) = plt.subplots(nrows=2, ncols=3)
to have ax{i}
as the matplotlib axes objects, or you may use the packed version
fig, ax_arr = plt.subplots(nrows=2, ncols=3)
ax_arr[0,0].plot(..) # plot to first top left axes
ax_arr[1,2].plot(..) # plot to last bottom right axes
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…