matlibplot: How to add space between some subplots

前端 未结 2 1249
再見小時候
再見小時候 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条回答
  •  青春惊慌失措
    2021-01-13 20:49

    To keep the solution close to your code you may use create 5 subplots with the middle one being one forth in height of the others and remove that middle plot.

    import matplotlib.pyplot as plt
    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, ax = plt.subplots(5,figsize=(7,7),sharex=True, 
                         gridspec_kw=dict(height_ratios=[4,4,1,4,4], hspace=0))
    
    ax[0].plot(x, y)
    ax[0].set_title('Panel: A')
    
    ax[1].plot(x, y**2)
    
    ax[2].remove()
    
    ax[3].plot(x, y**3)
    ax[3].set_title('Panel: B')
    ax[4].plot(x, y**4)
    
    
    plt.tight_layout()
    plt.show()
    

提交回复
热议问题