Solve matplotlib subplot overlapping problem

Before code modification:

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()

Effect before modification:
Insert image description here

After code modification:

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()

Effect after modification:

Insert image description here

Insert image description here

Guess you like

Origin blog.csdn.net/m0_67790374/article/details/131730563