代码之家  ›  专栏  ›  技术社区  ›  Commoner

Matplotlib子批次-意外的Y轴刻度

  •  0
  • Commoner  · 技术社区  · 8 年前

    我正在使用matplotlib子地块。这是我的代码框架:

    import matplotlib.pyplot as plt
    from matplotlib import gridspec
    
    plt.close('all')
    
    f, axarr = plt.subplots(2, sharex=True,)
    gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1]) 
    
    axarr[0] = plt.subplot(gs[0])   
    axarr[1] = plt.subplot(gs[1]) 
    
    axarr[0].set_ylim([-10,10])
    axarr[1].set_ylim([-1,1])
    
    plt.tight_layout()
    f.subplots_adjust(hspace=0)
    plt.show()
    

    这是我从这段代码中得到的输出。 enter image description here

    可以看到,在左侧的y轴上,我得到了相互重叠的ytick标签和右侧y轴上的“奇怪”y轴刻度标签(0)。我如何解决这个问题?我会很感激能在这里得到帮助。

    1 回复  |  直到 8 年前
        1
  •  1
  •   ImportanceOfBeingErnest    8 年前

    这些是上部子地块的x标签,仅部分被下部子地块隐藏。如果你愿意,把它们关掉,

    axarr[0].set_xticklabels([])
    

    为了使ticklabels不重叠,您可以更改轴的Y长度,

    axarr[0].set_ylim([-10.5,10])
    axarr[1].set_ylim([-1,1.2])