我有一個影像資料集,每條記錄包含2張影像,以及它們是否屬于同一類別(從時尚MNIST資料集建立)。
我想在每一對圖片上顯示標簽("匹配 "或 "不匹配")。
我的代碼:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
#從訓練集中挑選4個隨機對。
random_indices = np.random.randint(0, len(train_pairs), size=4)
random_pairs = train_pairs[random_indices]
random_distance = train_distance[random_indices]。
fig = plt.figure(figsize=(20, 10)
outer = gridspec.GridSpec(2, 2, wspace=0.2, hspace=0.2)
for i in range(4)。
inner = gridspec.GridSpecFromSubplotSpec(1, 2,
subplot_spec=outer[i], wspace=0.1, hspace=0.1)
for j in range(2)。
ax = plt.Subplot(fig, inner[j])
# 顯示圖片# 顯示標簽[/span
ax.text(0, 0, '{}'.format(random_distance[i])。
size=24, ha='center', va='center', color='w')
ax.set_xticks([])
ax.set_yticks([])
fig.add_subplot(ax)
fig.show()
我想要的是在每對影像之間的底部中心位置顯示 "匹配 "或 "不匹配 "的標簽。
uj5u.com熱心網友回復:
我試著用子圖作業,它給出了所需的結果,為每個子圖使用supxlabel。
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
#從訓練集中挑選4個隨機對。
random_indices = np.random.randint(0, len(train_pairs), size=4)
random_pairs = train_pairs[random_indices]
random_distance = train_distance[random_indices]。
fig = plt.fig(figsize=(20, 10)
subFigs = fig.subfigures(2, 2).flatten()
print(subFigs)
for i in range(4)。
subFig = subFigs[i] 。
label = "Match" if random_distance[i] else "miss-Match"
subFig.supxlabel(label, fontsize=16, color='red' )
axs = subFig.subplots(1, 2)
for j in range(2)。
ax = axs[j]
# show the image: ax = axs[j].
ax.imshow(random_pairs[i][j])
ax.set_xticks([])
ax.set_yticks([])
subFig.add_subplot(ax)
fig.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/332821.html
標籤:


