• matplotlib中的pyplot实用详解


    示例1:使用axes确定子图的位置

    在这里插入图片描述

    import matplotlib.pyplot as plt
    import numpy as np
    
    # 固定随机种子
    np.random.seed(0)
    
    # 2行1列的子图1
    plt.subplot(211)
    plt.imshow(np.random.random((10, 10)))
    # 2行1列的子图2
    plt.subplot(212)
    plt.imshow(np.random.random((10, 10)))
    
    plt.subplots_adjust(left=0, bottom=0, right=1, top=1) #调整子图的位置
    # [left, bottom, width, height]确定子图位置
    cax = plt.axes([0.8, 0, 0.06, 1])
    plt.colorbar(cax=cax)
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    相关函数详解

    1、plt.subplot()

    官网参考链接:matplotlib-pyplot-subplot
    输入的参数有以下4种情况:

    subplot(nrows, ncols, index, **kwargs)
    subplot(pos, **kwargs)
    subplot(**kwargs)
    subplot(ax)
    
    • 1
    • 2
    • 3
    • 4

            三个整数(nrows、ncols、index)。子图将占据nrows行和ncols列网格上的索引位置。索引从左上角的1开始,向右增加。index也可以是指定子时隙的(第一个,最后一个)索引(以1为基础,包括最后一个索引)的两元组,例如,fig.add_subplot(3,1,(1,2))构成了一个跨越图上部2/3的子时隙。

    加粗样式

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.transforms as mtransforms
    
    # 固定随机种子
    np.random.seed(0)
    
    # 确定画布的大小
    fig = plt.figure(figsize=(10,20))
    
    
    # plt.subplot(421)等价于plt(4,2,1),即3行两列中的第一个子图
    ax1 = plt.subplot(4, 2, 1, title="first subplot (4,2,1)")
    plt.imshow(np.random.random((10, 10)))
    
    # 这个子图没有边框
    ax2 = plt.subplot(422, frame_on=False, title="no frame subplot")
    plt.imshow(np.random.random((10, 10)))
    
    # 极坐标子图
    ax3 = plt.subplot(423, projection='polar', title="polar subplot")
    xs = np.arange(7)
    ys = xs**2
    trans_offset = mtransforms.offset_copy(ax3.transData, fig=fig, y=5, units='dots')
    '''
    x, y : float, default: 0.0 -> x,y方向上偏移的距离.
    '''
    for x, y in zip(xs, ys):
        plt.polar(x, y, 'ro')
        plt.text(x, y, '%d, %d' % (int(x), int(y)),
                transform=trans_offset,
                horizontalalignment='center',
                verticalalignment='bottom')
    
    # 添加红色背景,因为和ax2共享x轴的比例
    plt.subplot(424, sharex=ax2, facecolor='red', title="red facecolor subplot")
    plt.imshow(np.random.random((10, 10)))
    
    # (5,6)代表这个子图可以使用原本5,6这两个子图的空间,并于ax2共享比例,可以看到,比例是由最大的那个决定的
    plt.subplot(4,2,(5, 6), sharex=ax2, title="corss (5,6) subplot")
    plt.imshow(np.random.random((10, 28)))
    
    # 刻度线
    plt.subplot(4,2,7, title="about ticks change subplot 1")
    plt.minorticks_on() #开启微小刻度线
    plt.tick_params(which='major',width=4) #让主刻度线变粗
    
    
    # 刻度线
    plt.subplot(4,2,8, title="about ticks change subplot 2")
    plt.minorticks_off()
    plt.tick_params(top=True,bottom=False,left=False,right=False)
    plt.tick_params(labeltop=True,labelleft=False,labelright=False,labelbottom=False)
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    2、plt.subplots_adjust() 与 plt.axes()来控制子图的位置

            plt.subplots()能够自动确定子图的位置,但是不够灵活,而通过plt.subplots_adjust() 与 plt.axes()就可以实现子图任意位置的摆放。
            假设我现在有如下长条状的图片imgs:
    在这里插入图片描述
            然后通过plt.subplots_adjust() 与 plt.axes()我可以让多张这样的图片根据我喜欢的位置进行排列。其用法如下:
            plt.subplots_adjust():
            Parameters:
            left: float, optional
            子图左边缘的位置,作为图形宽度的一部分。
            right:float, optional
            子图右边缘的位置,作为图形宽度的一部分。
            bottom:float, optional
            子图底边缘的位置,作为图形宽度的一部分。
            top:float, optional
            子图顶边缘的位置,作为图形宽度的一部分。
            wspace:float, optional
            子图之间的填充宽度,作为平均轴宽度的一部分。
            hspace:float, optional
            子图之间的填充高度,作为平均轴宽度的一部分。

            plt.axes()
            最主要的Parameters:
            rect: [left, bottom, width, height] -> 分别代表子图的左下点位置,以及子图的宽、高。
    在这里插入图片描述

    # 画图
    def imshow(imgs):
        imgs = imgs / 2.0 + 0.5  # 逆归一化,像素值从[-1, 1]回到[0, 1]
        imgs = imgs.cpu().numpy().transpose((1, 2, 0))  # 图像从(C, H, W)转回(H, W, C)的numpy矩阵
        plt.axis("off")
        plt.imshow(imgs)# 这一行不会讲图片显示出来,需要plt.show()才会最终将图片显示
    
    # 添加一个覆盖整个窗口的子图,背景色为灰色
    plt.axes(facecolor='grey')
    # 取消标签
    plt.tick_params(top=False,bottom=False,left=False,right=False)
    plt.tick_params(labeltop=False,labelleft=False,labelright=False,labelbottom=False)
    
    plt.subplots_adjust(left=0, bottom=0, right=1, top=1)
    plt.axes([0, 0.9, 1, 0.15])
    imshow(imgs)
    
    plt.axes([0, 0.75, 1, 0.15])
    imshow(imgs)
    
    plt.axes([0, 0.6, 1, 0.15])
    imshow(imgs)
    
    plt.axes([0, 0.45, 1, 0.15])
    imshow(imgs)
    
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    【OpenCV-Python】教程:3-8 图像金字塔
    TCP和UDP的由浅到深的详细讲解
    SAP AIF BTI750
    Docker启动mysql服务
    plink的文件格式
    基于html和vue的科技星球展示网页设计
    【高并发】SimpleDateFormat类到底为啥不是线程安全的?(附六种解决方案,建议收藏)
    数组扁平化实现
    uniapp获取公钥、MD5,‘keytool‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
    leetcode11-盛最多水的容器
  • 原文地址:https://blog.csdn.net/shizuguilai/article/details/127682561