• plt.subplot()函数解析(最清晰的解释)


    欢迎关注WX公众号:【程序员管小亮】

    plt.subplot()函数用于直接指定划分方式和位置进行绘图。

    MATLABMATLABpyplotpyplot有当前的图形(figurefigure)和当前的轴(axesaxes)的概念,所有的作图命令都是对当前的对象作用。可以通过gca()gca()获得当前的轴(axesaxes),通过gcf()gcf()获得当前的图形(figurefigure)。

    # 使用plt.subplot来创建小图. plt.subplot(221)表示将整个图像窗口分为2行2列, 当前位置为1.
    plt.subplot(221)
    # plt.subplot(222)表示将整个图像窗口分为2行2列, 当前位置为2.
    plt.subplot(222) # 第一行的右图
    # plt.subplot(223)表示将整个图像窗口分为2行2列, 当前位置为3.
    plt.subplot(223)
    # plt.subplot(224)表示将整个图像窗口分为2行2列, 当前位置为4.
    plt.subplot(224)
    

    注意:其中各个参数也可以用逗号,分隔开。第一个参数代表子图的行数;第二个参数代表该行图像的列数; 第三个参数代表每行的第几个图像。

    import numpy as np
    import matplotlib.pyplot as plt
    
    def f(t):
    	return np.exp(-t) * np.cos(2*np.pi*t)
    
    t1 = np.arange(0.0, 5.0, 0.1)
    t2 = np.arange(0.0, 5.0, 0.02)
    plt.figure(1)
    plt.subplot(211)
    plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
    plt.subplot(212)
    plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
    plt.show()
    

    在这里插入图片描述
    如果不指定figure()figure()的轴,figure(1)figure(1)命令默认会被建立,同样的如果你不指定subplot(numrows,numcols,fignum)subplot(numrows, numcols, fignum)的轴,subplot(111)subplot(111)也会自动建立。

    import matplotlib.pyplot as plt
    plt.figure(1) # 创建第一个画板(figure)
    plt.subplot(211) # 第一个画板的第一个子图
    plt.plot([1, 2, 3])
    plt.subplot(212) # 第二个画板的第二个子图
    plt.plot([4, 5, 6])
    plt.figure(2) # 创建第二个画板
    plt.plot([4, 5, 6]) # 默认子图命令是subplot(111)
    plt.figure(1) # 调取画板1; subplot(212)仍然被调用中
    plt.subplot(211) # 调用subplot(211)
    plt.title('Easy as 1, 2, 3') # 做出211的标题
    

    在这里插入图片描述
    在这里插入图片描述

    python课程推荐。
    在这里插入图片描述

  • 相关阅读:
    MSP430F149学习之路——蓝牙模块
    MSP430F149学习之路——SPI
    MSP430推荐网站
    MSP430F149学习之路——UART
    MSP430F149学习之路——比较器Comparaor_A
    MSP430F149学习之路——PWM信号
    MSP430F149学习之路——捕获/比较模式
    MSP430F149学习之路——时钟1
    CUDA学习笔记(三)——CUDA内存
    CUDA学习笔记(一)——CUDA编程模型
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13302801.html
Copyright © 2020-2023  润新知