zoukankan      html  css  js  c++  java
  • matplotlib的使用——subplot分格显示

    分格显示的方法

    分格显示常用的方法有两种,一种是利用plt.subplot2grid对图像进行网格化,划分的同时分配空间;另一种利用gridspec.GridSpec进行网格化,划分完后分配空间。

    利用plt.subplot2grid进行分格显示

    该方法的关键是利用plt.subplot2grid对图像进行网格化,划分的同时分配空间。
    应用方式如下:

    # 创建图像
    plt.figure()
    
    # 对图像进行网格化,(3,3)代表将图像分成3X3的网格
    # (0,0)代表是起始点,rowspan代表向下的行数,colspan向右的列数
    sub1 = plt.subplot2grid((3,3),(0,0),rowspan = 1,colspan = 3)
    # 划分后的图像参数设置方式,需要加上set
    sub1.set_xlabel("I am x label")
    sub1.set_xticks(np.linspace(-1,1,11))
    sub1.plot(x,y1,color = 'red',linewidth = 1.0,linestyle = '--',marker = '.')
    
    sub2 = plt.subplot2grid((3,3),(1,0),rowspan = 2,colspan = 1)
    sub2.plot(x,y2,color = 'pink',linewidth = 1.,linestyle = '-.',marker = 'o')
    
    sub3 = plt.subplot2grid((3,3),(1,1),rowspan = 2,colspan = 2)
    sub3.plot(x,y3,color = 'green',linewidth = 1.,linestyle = '--',marker = '*')
    

    其中:
    1、subplot2grid用于对图像进行网格化,(3,3)代表将图像分成3X3的网络,(0,0)代表是起始点,rowspan代表向下的行数,colspan向右的列数。
    2、图像标签label,坐标tick的设置方式与常规plot图不同,需要用方法进行设置,同时需要加上方法名称要加上set_。
    绘画结果为:
    在这里插入图片描述

    在这里插入图片描述

    利用gridspec.GridSpec进行分格显示

    该方法的关键是利用gridspec.GridSpec进行网格化,划分完后分配空间。
    应用方法如下:

    plt.figure()
    
    # 对网格进行3X3的划分
    gs = gridspec.GridSpec(3, 3)
    
    # 像数组一样分配
    sub4 = plt.subplot(gs[0, :3])
    
    sub5 = plt.subplot(gs[1:,0])
    
    sub6 = plt.subplot(gs[1:,1:])
    

    其中:
    1、利用gridspec.GridSpec(3, 3)对网格进行3X3的划分。
    2、数组一样分配网格。

    在这里插入图片描述

    应用示例

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.gridspec as gridspec
    
    x = np.linspace(-1,1,50)
    y1 = 2*x + 1
    y2 = x**2
    y3 = np.sin(x)
    
    # 创建图像
    plt.figure()
    
    # 对图像进行网格化,(3,3)代表将图像分成3X3的网格
    # (0,0)代表是起始点,rowspan代表向下的行数,colspan向右的列数
    sub1 = plt.subplot2grid((3,3),(0,0),rowspan = 1,colspan = 3)
    # 划分后的图像参数设置方式,需要加上set
    sub1.set_xlabel("I am x label")
    sub1.set_xticks(np.linspace(-1,1,11))
    sub1.plot(x,y1,color = 'red',linewidth = 1.0,linestyle = '--',marker = '.')
    
    sub2 = plt.subplot2grid((3,3),(1,0),rowspan = 2,colspan = 1)
    sub2.plot(x,y2,color = 'pink',linewidth = 1.,linestyle = '-.',marker = 'o')
    
    sub3 = plt.subplot2grid((3,3),(1,1),rowspan = 2,colspan = 2)
    sub3.plot(x,y3,color = 'green',linewidth = 1.,linestyle = '--',marker = '*')
    
    
    plt.figure()
    
    # 对网格进行3X3的划分
    gs = gridspec.GridSpec(3, 3)
    
    # 像数组一样分配
    sub4 = plt.subplot(gs[0, :3])
    
    sub5 = plt.subplot(gs[1:,0])
    
    sub6 = plt.subplot(gs[1:,1:])
    
    plt.show()
    

    利用plt.subplot 进行分格显示


    这里写图片描述

    plt.subplot(2,1,1)会将原始的图像切割成2个子图像,是2行1列,并将现在的操作位置转到第一个子图上,这样便实现了绘制子图的方法。

    subplot在指定分割子图个数和定位子图时可以使用参数连写的方式如:plt.subplot(221)

    天道酬勤 循序渐进 技压群雄
  • 相关阅读:
    hdu 1.2.4
    交换机&&路由器
    AP、AC、无线路由器
    肩胛骨
    无线路由器
    背部肌肉
    胸部肌肉
    redis未授权访问
    进制
    攻防实验
  • 原文地址:https://www.cnblogs.com/wuyuan2011woaini/p/15681945.html
Copyright © 2011-2022 走看看