グラフのラベルが重ならないよう自動調整

調整した場合:

調整しない場合:

fig.tight_layout() とすることで、グラフ同士のラベルが重ならない程度にグラフを小さくする。

#!/usr/bin/env python


import matplotlib.pyplot as plt


def plot():
    fig = plt.figure()

    for r in range(3):
        for c in range(4):
            ax = fig.add_subplot(3,4,r*4+c)

            ax.set_title("Title")
            ax.set_ylabel("Y Label")
            ax.set_xlabel("X Label")
    fig.tight_layout()
    
    plt.savefig('test.png')


if __name__=="__main__":
    plot()


※参照: http://matplotlib.org/users/tight_layout_guide.html