Pylab - Adjust hspace for some of the subplots

前端 未结 1 1554
面向向阳花
面向向阳花 2020-12-18 07:58

I have a plot in which I want to have one panel separate from other four panels. I want the rest of the four panels to share the x axis. The figure is shown below. I want th

相关标签:
1条回答
  • 2020-12-18 08:21

    It's easiest to use two separate gridspec objects for this. That way you can have independent margins, padding, etc for different groups of subplots.

    As a quick example:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # We'll use two separate gridspecs to have different margins, hspace, etc
    gs_top = plt.GridSpec(5, 1, top=0.95)
    gs_base = plt.GridSpec(5, 1, hspace=0)
    fig = plt.figure()
    
    # Top (unshared) axes
    topax = fig.add_subplot(gs_top[0,:])
    topax.plot(np.random.normal(0, 1, 1000).cumsum())
    
    # The four shared axes
    ax = fig.add_subplot(gs_base[1,:]) # Need to create the first one to share...
    other_axes = [fig.add_subplot(gs_base[i,:], sharex=ax) for i in range(2, 5)]
    bottom_axes = [ax] + other_axes
    
    # Hide shared x-tick labels
    for ax in bottom_axes[:-1]:
        plt.setp(ax.get_xticklabels(), visible=False)
    
    # Plot variable amounts of data to demonstrate shared axes
    for ax in bottom_axes:
        data = np.random.normal(0, 1, np.random.randint(10, 500)).cumsum()
        ax.plot(data)
        ax.margins(0.05)
    
    plt.show()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题