zoukankan      html  css  js  c++  java
  • python在一个画布上画多个子图

    转载:https://blog.csdn.net/qq_26449287/article/details/103022636

    matplotlib 是可以组合许多的小图, 放在一张大图里面显示的. 使用到的方法叫作 subplot.

    均匀画图

    使用import导入matplotlib.pyplot模块, 并简写成plt. 使用plt.figure创建一个图像窗口.

    1.  
      import matplotlib.pyplot as plt
    2.  
       
    3.  
      plt.figure()

    使用plt.subplot来创建小图. plt.subplot(2,2,1)表示将整个图像窗口分为2行2列, 当前位置为1. 使用plt.plot([0,1],[0,1])在第1个位置创建一个小图.

    1.  
      plt.subplot(2,2,1)
    2.  
      plt.plot([0,1],[0,1])

    plt.subplot(2,2,2)表示将整个图像窗口分为2行2列, 当前位置为2. 使用plt.plot([0,1],[0,2])在第2个位置创建一个小图.

    1.  
      plt.subplot(2,2,2)
    2.  
      plt.plot([0,1],[0,2])

    plt.subplot(2,2,3)表示将整个图像窗口分为2行2列,当前位置为3. plt.subplot(2,2,3)可以简写成plt.subplot(223), matplotlib同样可以识别. 使用plt.plot([0,1],[0,3])在第3个位置创建一个小图.

    1.  
      plt.subplot(223)
    2.  
      plt.plot([0,1],[0,3])

    plt.subplot(224)表示将整个图像窗口分为2行2列, 当前位置为4. 使用plt.plot([0,1],[0,4])在第4个位置创建一个小图.

    1.  
      plt.subplot(224)
    2.  
      plt.plot([0,1],[0,4])
    3.  
       
    4.  
      plt.show() # 展示

    不均匀画图

    如果希望展示的小图的大小不相同, 应该怎么做呢? 以上面的4个小图为例, 如果把第1个小图放到第一行, 而剩下的3个小图都放到第二行.

    使用plt.subplot(2,1,1)将整个图像窗口分为2行1列, 当前位置为1. 使用plt.plot([0,1],[0,1])在第1个位置创建一个小图.

    1.  
      plt.subplot(2,1,1)
    2.  
      plt.plot([0,1],[0,1])

    使用plt.subplot(2,3,4)将整个图像窗口分为2行3列, 当前位置为4. 使用plt.plot([0,1],[0,2])在第4个位置创建一个小图.

    1.  
      plt.subplot(2,3,4)
    2.  
      plt.plot([0,1],[0,2])

    这里需要解释一下为什么第4个位置放第2个小图. 上一步中使用plt.subplot(2,1,1)将整个图像窗口分为2行1列, 第1个小图占用了第1个位置, 也就是整个第1行. 这一步中使用plt.subplot(2,3,4)将整个图像窗口分为2行3列, 于是整个图像窗口的第1行就变成了3列, 也就是成了3个位置, 于是第2行的第1个位置是整个图像窗口的第4个位置.

    使用plt.subplot(235)将整个图像窗口分为2行3列,当前位置为5. 使用plt.plot([0,1],[0,3])在第5个位置创建一个小图. 同上, 再创建plt.subplot(236).

    1.  
      plt.subplot(235)
    2.  
      plt.plot([0,1],[0,3])
    3.  
       
    4.  
      plt.subplot(236)
    5.  
      plt.plot([0,1],[0,4])
    6.  
       
    7.  
      plt.show() # 展示

    完整代码

    1.  
      plt.figure()
    2.  
      plt.subplot(2,1,1)
    3.  
      plt.plot([0,1],[0,1])
    4.  
       
    5.  
      plt.subplot(2,3,4)
    6.  
      plt.plot([0,1],[0,2])
    7.  
       
    8.  
      plt.subplot(235)
    9.  
      plt.plot([0,1],[0,3])
    10.  
       
    11.  
      plt.subplot(236)
    12.  
      plt.plot([0,1],[0,4])
    13.  
       
    14.  
      plt.show() # 展示

    结果如下

  • 相关阅读:
    增强遍历和Object多参数遍历
    Git忽略规则(.gitignore配置)不生效原因和解决
    算法基本概念及常用算法Python实现
    使用GitBook编写项目文档
    Python 闭包
    Linux 进程管理
    Kafka 安装及入门
    IP地址0.0.0.0表示什么
    Docker 入门
    Docker Linux下安装
  • 原文地址:https://www.cnblogs.com/MCSFX/p/13638096.html
Copyright © 2011-2022 走看看