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: 第三種方法

    # method4
    plt.figure()
    plt.subplot(2, 2, 1)
    plt.plot([0, 1], [0, 1])
    plt.subplot(222)
    plt.plot([0, 1], [0, 2])
    plt.subplot(223)
    plt.plot([0, 1], [0, 3])
    plt.subplot(224)
    plt.plot([0, 1], [0, 4])
    plt.savefig('./image_dir/grid4.png')
    plt.tight_layout()
    plt.show()

4: 第四種

 # 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()

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM