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

変更前の効果:
ここに画像の説明を挿入します

コード変更後:

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

変更後の効果:

ここに画像の説明を挿入します

ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/m0_67790374/article/details/131730563