cs 231n assignment 1
plt.subplot()
函数用于直接指定划分方式和位置进行绘图
# subplot(numRows, numCols, plotNum)
# or subplot(numRows numCols plotNum)
图表的整个绘图区域被分成 numRows
行和 numCols
列
然后按照从左到右,从上到下的顺序对每个子区域进行编号,左上的子区域的编号为1
# if numRows = 3, numCols = 4
# plotNum:
# 1 2 3 4
# 5 6 7 8
# 9 10 11 12
想要对哪个位置的图片进行编辑,只需要对应的plotNum = num
如果 numRows
, numCols
和 plotNum
这三个数都小于 10 的话, 可以把它们缩写为一个整数, 例如 subplot(323)
和 subplot(3,2,3)
是相同的。
example1
画出(1,1)、(1,2)、(2,1)、(2,2)共四个小图
subplot(2, 2, 1)
subplot(2, 2, 2)
subplot(2, 2, 3)
subplot(2, 2, 4)
example2
画出(1,1)、(1,2)、(2,1)+(2,2)共三个小图
subplot(2, 2, 1)
subplot(2, 2, 2)
subplot(2, 1, 2) # 重新划分为两行一列的第二个图片
example3
# Visualize some examples from the dataset.
# We show a few examples of training images from each class.
classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
num_classes = len(classes)
samples_per_class = 7
for y, cls in enumerate(classes):
idxs = np.flatnonzero(y_train == y)
idxs = np.random.choice(idxs, samples_per_class, replace=False)
for i, idx in enumerate(idxs):
plt_idx = i * num_classes + y + 1
plt.subplot(samples_per_class, num_classes, plt_idx)
plt.imshow(X_train[idx].astype('uint8'))
plt.axis('off')
if i == 0:
plt.title(cls)
plt.show()
画出samples_per_class = 7
行,num_class = 10
列
例如,y = 1
i = 1
时,plt.idx = 1*10+1+1=12
就是第二列第二行图片
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
...
...