Improve subplot size/spacing with many subplots in matplotlib

前端 未结 6 1657
一整个雨季
一整个雨季 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

    Similar to tight_layout matplotlib now (as of version 2.2) provides constrained_layout. In contrast to tight_layout, which may be called any time in the code for a single optimized layout, constrained_layout is a property, which may be active and will optimze the layout before every drawing step.

    Hence it needs to be activated before or during subplot creation, such as figure(constrained_layout=True) or subplots(constrained_layout=True).

    Example:

    import matplotlib.pyplot as plt
    
    fig, axes = plt.subplots(4,4, constrained_layout=True)
    
    plt.show()
    

    constrained_layout may as well be set via rcParams

    plt.rcParams['figure.constrained_layout.use'] = True
    

    See the what's new entry and the Constrained Layout Guide

提交回复
热议问题