解决matplotlib子图重叠问题

代码修改前:

import matplotlib.pyplot as plt
import seaborn as sns

def on_resize(event):
    print('当前画布大小为:{}x{}'.format(event.width, event.height))

if __name__ == '__main__':
    x = list(range(1, 6))
    y1 = [i ** 2 for i in x]
    y2 = [i ** 3 for i in x]

    fig = plt.figure()
    fig.canvas.mpl_connect('resize_event', on_resize)
    plt.title('Title')
    plt.axis('off')
    for i in range(321, 327):
        ax = fig.add_subplot(i)
        sns.kdeplot(y1, color='red', fill=True)
        sns.kdeplot(y2, color='blue', fill=True)
        ax.set_title('Subtitle')
        ax.set_xlabel('X Label')
        ax.set_ylabel('Y Label')
        ax.legend(['y1', 'y2'])

    plt.show()

修改前效果:
解决matplotlib子图重叠问题_第1张图片

代码修改后:

import matplotlib.pyplot as plt
import seaborn as sns

def on_resize(event):
    print('当前画布大小为:{}x{}'.format(event.width, event.height))

if __name__ == '__main__':
    x = list(range(1, 6))
    y1 = [i ** 2 for i in x]
    y2 = [i ** 3 for i in x]
    parameters = {'figure.titlesize': 8, 'axes.titlesize': 8, 'axes.labelsize': 8, 'xtick.labelsize': 8, 'ytick.labelsize': 8, 'legend.fontsize': 8, 'legend.title_fontsize': 8}
    plt.rcParams.update(parameters)
    fig = plt.figure()
    fig.canvas.mpl_connect('resize_event', on_resize)
    plt.title('Title')
    plt.axis('off')
    for i in range(321, 327):
        ax = fig.add_subplot(i)
        sns.kdeplot(y1, color='red', fill=True)
        sns.kdeplot(y2, color='blue', fill=True)
        ax.set_title('Subtitle')
        ax.set_xlabel('X Label')
        ax.set_ylabel('Y Label')
        ax.legend(['y1', 'y2'])
    fig.tight_layout()
    plt.show()

修改后效果:

解决matplotlib子图重叠问题_第2张图片

解决matplotlib子图重叠问题_第3张图片

你可能感兴趣的:(python,matplotlib,子图重叠)