Changing matplotlib subplot size/position after axes creation

前端 未结 3 1403
难免孤独
难免孤独 2021-02-04 01:05

Is it possible to set the size/position of a matplotlib subplot after the axes are created? I know that I can do:

import matplotlib.pyplot as plt

ax = plt.subp         


        
3条回答
  •  执念已碎
    2021-02-04 02:00

    Thanks to Molly pointing me in the right direction, I have a solution:

    import matplotlib.pyplot as plt
    import matplotlib.gridspec as gridspec
    
    fig = plt.figure()
    
    ax = fig.add_subplot(111)
    
    gs = gridspec.GridSpec(3,1)
    ax.set_position(gs[0:2].get_position(fig))
    ax.set_subplotspec(gs[0:2])              # only necessary if using tight_layout()
    
    fig.add_subplot(gs[2])
    
    fig.tight_layout()                       # not strictly part of the question
    
    plt.show()
    

提交回复
热议问题