plt.subplots中的ax = ax.flatten()
阿新 • • 發佈:2018-12-06
在用plt.subplots畫多個子圖中,ax = ax.flatten()將ax由n*m的Axes組展平成1*nm的Axes組
以下面的例子說明ax = ax.flatten()的作用:
fig, ax = plt.subplots(nrows=2,ncols=2,sharex='all',sharey='all') ax = ax.flatten() for i in range(4): img = image[i].reshape(28, 28) ax[i].imshow(img, cmap='Greys', interpolation='nearest') # 區別:可以直接用ax[i]
不使用ax = ax.flatten()
fig, ax = plt.subplots(nrows=2,ncols=2,sharex='all',sharey='all') for i in range(4): img = image[i].reshape(28, 28) axs[0, 0].imshow(img, cmap='Greys', interpolation='nearest') # 區別:不能直接使用ax[i] axs[0, 1].imshow(img, cmap='Greys', interpolation='nearest') axs[1, 0].imshow(img, cmap='Greys', interpolation='nearest') axs[1, 1].imshow(img, cmap='Greys', interpolation='nearest')