pyplot.subplot 及 imshow绘图 去除坐标轴及调整子图间距

本文介绍了如何使用pyplot的subplot功能创建子图,并讲解了如何去除坐标轴以及调整子图之间的间距。通过示例说明了在设置子图参数时,left、right、bottom和top的含义及其要求,强调了这些值必须是相对于figure尺寸的分数,并且从figure的左下角开始测量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fnum = len(train_datasets)
plt.figure("show pickle", figsize=(10,10))
j = 0
for set_filename in train_datasets:
    print('trying to read pickle: %s '  % set_filename)
    try:
        with open(set_filename, 'rb') as f:
            dataset=pickle.load(f)
    except Exception as e:
        print('Unable to open pckle file ', set_filename, ':', e)
    print ('%d images in total in file %s' % (len(dataset), set_filename))  
    for i in range(10):
        plt.subplot( fnum, 10, 10*j + i + 1)
        plt.imshow(dataset[i], cmap='gray');
	##去除子图的坐标轴, 对上一个画出的子图起作用
        plt.axis('off')
j += 1

##设定子图间距 , left < right, top > bottom, 数字表示窗口大小的比例(如下则子图间距为窗口大小的1%) 
plt.subplots_adjust(left=0.04, top= 0.96, right = 0.96, bottom = 0.04, wspace = 0.01, hspace = 0.01)

关于为什么要了left<right (否则会报错 ValueError: bottom cannot be >= top):

When using subplots_adjust, the values of leftrightbottom and top are to be provided as fractions of the figure width and height. In additions, all values are measured from the left and bottom edges of the figure. This is why right and top can't be lower than left and right. A typical set-up is:  ----- 点击打开参考源



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值