Matplotlib.pyplot繪制子圖


subplots() + Axes

先上代碼

import numpy as np
import matplotlib.pyplot as plt

X_train = np.random.randn(25, 784) # 784 = 28 * 28
fig, ax = plt.subplots(            # subplots()返回一個(gè)Figure,和一個(gè)或一組Axes
    nrows=5,                       # 這里是返回5 * 5的Axes
    ncols=5,
    sharex=True,                   # 共享坐標(biāo)軸
    sharey=True, )

i = 0
ax = ax.flatten()                  # flatten()將ax由5*5的Axes組展平成1*25的Axes組
for img in X_train:                # 每一個(gè)img是1*784的張量
  if i > 24:
    break
  img = img.reshape(28, 28)        # 將img從1*784重構(gòu)成28*28的張量
  ax[i].imshow(img, cmap='Greys', interpolation='nearest')
  i += 1                           # imshow()以圖片形式顯示


ax[0].set_xticks([])               # 設(shè)置需要顯示的坐標(biāo)標(biāo)記,例如方括號(hào)可填[1,10,20]
ax[0].set_yticks([])               # 此時(shí)坐標(biāo)軸上1,10,20的位置就會(huì)顯示標(biāo)記
plt.tight_layout()                 # tight_layout()使得Figure上的Axes緊密排列,留出的空白少
plt.show()
import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(2, 2)                     # axs為2*2的Axes組

axs[0, 0].imshow(np.random.random((100, 100)))

axs[0, 1].imshow(np.random.random((100, 100)))

axs[1, 0].imshow(np.random.random((100, 100)))

axs[1, 1].imshow(np.random.random((100, 100)))

plt.subplot_tool()                              # subplot_tool()是調(diào)節(jié)Axes顯示的工具
plt.show()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容