matlibplot: How to add space between some subplots

前端 未结 2 1242
再見小時候
再見小時候 2021-01-13 20:34

How can I adjust the whitespace between some subplots? In the example below, let\'s say I want to eliminate all whitespace between the 1st and 2nd subplots as well as betwe

2条回答
  •  旧时难觅i
    2021-01-13 20:58

    You would need to use GridSpec to have different spaces between plots:

    import matplotlib.pyplot as plt
    import matplotlib.gridspec as gridspec
    import numpy as np
    
    # Simple data to display in various forms
    x = np.linspace(0, 2 * np.pi, 400)
    y = np.sin(x ** 2)
    
    f = plt.figure(figsize=(10,10))
    gs0 = gridspec.GridSpec(2, 1)
    
    gs00 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[0], hspace=0)
    ax0 = f.add_subplot(gs00[0])
    ax0.plot(x, y)
    ax0.set_title('Panel: A')
    ax1 = f.add_subplot(gs00[1], sharex=ax0)
    ax1.plot(x, y**2)
    
    gs01 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[1], hspace=0)
    ax2 = f.add_subplot(gs01[0])
    ax2.plot(x, y**3)
    ax2.set_title('Panel: B')
    ax3 = f.add_subplot(gs01[1], sharex=ax0)
    ax3.plot(x, y**4)
    
    plt.show()
    

提交回复
热议问题