matplotlib subplot 多图合一

时间:2020-01-12
本文章向大家介绍matplotlib subplot 多图合一,主要包括matplotlib subplot 多图合一使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1:第一种方法

# method1: subplot2grid
    #################
    '''
    第一个参数(3, 3) 是把图分成3行3列
    第二个参数是位置 (0, 0)表示从0行0列开始
    第三个参数  colspan=3  表示列占3列 ,
    第四个参数 rowspan=1  表示行占一行

    '''
    plt.figure()
    ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=1)
    ax1.plot([1, 2], [1, 2])
    ax1.set_title('al1_title')
    ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2,)
    ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
    ax4 = plt.subplot2grid((3, 3), (2, 0))
    ax5 = plt.subplot2grid((3, 3), (2, 1))

    plt.savefig('./image_dir/grid1.png')
    plt.show()

2: 第二种方法:

    import matplotlib.pyplot as plt
    import matplotlib.gridspec as gridspec

    plt.figure()
    gs = gridspec.GridSpec(3, 3)
    ax1 = plt.subplot(gs[0, :])
    ax2 = plt.subplot(gs[1, :2])
    ax3 = plt.subplot(gs[1:, 2])
    ax4 = plt.subplot(gs[-1, 0])
    ax5 = plt.subplot(gs[-1, -2])

    plt.savefig('./image_dir/grid2.png')
    plt.show()

3: 第三种方法

 # method 3 : easy to define structure
    f, ((ax11, ax12), (ax21, ax22)) = plt.subplots(2, 2, sharex=True, sharey=True)
    ax11.scatter([1, 2], [1, 2])
    plt.savefig('./image_dir/grid3.png')
    plt.tight_layout()
    plt.show()

原文地址:https://www.cnblogs.com/heguihui/p/12183302.html