Improve subplot size/spacing with many subplots in matplotlib

前端 未结 6 1542
一整个雨季
一整个雨季 2020-11-22 12:56

Very similar to this question but with the difference that my figure can be as large as it needs to be.

I need to generate a whole bunch of vertically-stacked plots

6条回答
  •  花落未央
    2020-11-22 13:03

    I found that subplots_adjust(hspace = 0.001) is what ended up working for me. When I use space = None, there is still white space between each plot. Setting it to something very close to zero however seems to force them to line up. What I've uploaded here isn't the most elegant piece of code, but you can see how the hspace works.

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.ticker as tic
    
    fig = plt.figure()
    
    x = np.arange(100)
    y = 3.*np.sin(x*2.*np.pi/100.)
    
    for i in range(5):
        temp = 510 + i
        ax = plt.subplot(temp)
        plt.plot(x,y)
        plt.subplots_adjust(hspace = .001)
        temp = tic.MaxNLocator(3)
        ax.yaxis.set_major_locator(temp)
        ax.set_xticklabels(())
        ax.title.set_visible(False)
    
    plt.show()
    

    enter image description here

提交回复
热议问题