[Python] seaborn pairplot title savefig [duplicate]

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    With the below code I can prepare my pair plots on google-colab, and add one title for the whole chart, as far everything is OK. The y variable in g.fig.suptitle() helped to move the title slightly above the chart it is also fine.

    Nevertheless, while saving the chart as an image, the title gets to the top edge (the top part of the displayed charts seems to be cut off) and it is hardly to read on the saved png file. How can I add top margin or anything else that would leave the title and some space above it? I have already tried adding bbox_inches='auto' to savefig() but it did not work not even run.

    import matplotlib.pyplot as plt
    import seaborn as sns
    from google.colab import files


    plt.figure(figsize=(20, 20)) # figsize in inches (width, heights)

    # ‘kde’, ‘hist’, ‘reg’
    g = sns.pairplot(df, diag_kind="hist")
    g.fig.suptitle("Pairwise Plots", y=1, fontsize=14)

    #g.fig.savefig('eda_pairwise_hist.png', bbox_inches='auto', pad_inches=1)
    g.fig.savefig('eda_pairwise_hist.png', pad_inches=3)
    files.download('eda_pairwise_hist.png')


    Solution: Adding g.fig.subplots_adjust(top=0.95) as suggested by @tmdavison. The whole code listing:


    import matplotlib.pyplot as plt
    import seaborn as sns


    plt.figure(figsize=(20, 20)) # figsize in inches (width, heights)

    # ‘kde’, ‘hist’, ‘reg’
    g = sns.pairplot(df[meta_data + summary_ravenscroft], diag_kind="hist")
    g.fig.subplots_adjust(top=0.95)
    g.fig.suptitle("Pairwise Plots", fontsize=14)

    g.fig.savefig('eda_pairwise_hist.png', pad_inches=3)
    files.download('eda_pairwise_hist.png')

    Continue reading...

Compartilhe esta Página