plt.subplot()

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, numColsplotNum 这三个数都小于 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 
...
...
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值