Matplotlib subplot y-axis scale overlaps with plot above

前端 未结 2 592
醉梦人生
醉梦人生 2021-01-06 02:24

I am trying to plot 3 subplots without any white space between them. The default y axis ticklabels use a scale displayed to the top right of the y axis (1e-8 in the example

相关标签:
2条回答
  • 2021-01-06 03:11

    Ok, this is probably an ugly solution but you could just simply upscale your data in the lower plots with the range of the power i.e. ax0b.plot(x, y*1e8). This works for your example at least.

    0 讨论(0)
  • 2021-01-06 03:28

    I have two options you might want to look at.

    First, set the axis location and size yourself as such:

    # your imports and data above
    fig = plt.figure()
    ax0a = fig.add_axes([0.1, 0.1, 0.8, 0.25])
    ax0b = fig.add_axes([0.1, 0.39, 0.8, 0.25], sharex=ax0a)
    ax0c = fig.add_axes([0.1, 0.68, 0.8, 0.25], sharex=ax0a)
    ax0a.set_xticklabels([])
    ax0b.set_xticklabels([]) 
    ax0a.plot(x,y)
    nbins = len(ax0a.get_xticklabels())
    ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
    ax0b.plot(x,y)
    ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
    ax0c.plot(x,y)
    ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
    plt.show()
    

    enter image description here

    The second option is to manually adjust the location and maybe font size of the offset text:

    # your original code minus data and imports
    fig = plt.figure()
    gs1 = gridspec.GridSpec(3, 3)
    gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0)
    ax0a = plt.subplot(gs1[0, :])
    ax0b = plt.subplot(gs1[1, :])
    ax0c = plt.subplot(gs1[2, :])
    ax0a.set_xticklabels([])
    ax0b.set_xticklabels([]) 
    ax0a.plot(x,y)
    nbins = len(ax0a.get_xticklabels())
    ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
    ax0b.plot(x,y)
    ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
    ax0c.plot(x,y)
    ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
    
    # play around with location and font of offset text here
    ax0a.get_yaxis().get_offset_text().set_x(-0.075)
    ax0a.get_yaxis().get_offset_text().set_size(10)
    ax0b.get_yaxis().get_offset_text().set_x(-0.075)
    ax0b.get_yaxis().get_offset_text().set_size(10)
    ax0c.get_yaxis().get_offset_text().set_x(-0.075)
    ax0c.get_yaxis().get_offset_text().set_size(10)
    plt.show()
    

    enter image description here

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