How to adjust space between Matplotlib / Seaborn subplots for multi-line layouts

The following figure shows standard Seaborn / Matplotlib frames in a 2 X 2 grid layout:

seaborn / matplotlib sample boxplots

This is pretty much what I want, except that I would like to add some more space between the first line of the plots and the second line. The distance between the X-axis labels of the graphs of the first line and the names of the sections of the second line is almost non-existent. I played around with parameters as described in this thread:

Stackoverflow question

Here is my code:

    import math
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.backends.backend_pdf import PdfPages
    from PyPDF2 import PdfFileMerger
    import seaborn as sns
    num_cols = 2
    num_rows = int(math.ceil(tot_plots / float(num_cols)))
    fig, axes = plt.subplots(nrows=num_rows, ncols=num_cols, figsize=(16, 16))
    x_var = df_orig['hra']
    for idx, ax in enumerate(axes.flat):
        data_var = current_cols[idx]
        y_var = df_orig[data_var]
        title_str = ''
        sns.boxplot(x=x_var, y=y_var, ax=ax,
                    order=order, palette=color, showfliers=False)
        ax.set_title(data_var + title_str)
        ax.xaxis.label.set_visible(False)
        ax.yaxis.label.set_visible(False)
        ax.xaxis.set_tick_params(labelsize=8)
        ax.yaxis.set_tick_params(labelsize=8)
        plt.setp(ax.xaxis.get_majorticklabels(), rotation=90)
    fig.suptitle("Sampling BoxPlots", x=0.5, y=0.93, fontsize=14, fontweight="bold")
    plt.tight_layout()
    plt.subplots_adjust(top=0.8)
    pdf_pages = PdfPages(file_name)
    pdf_pages.savefig()
    pdf_pages.close()

      

+3


source to share


1 answer


Have you tried customizing hspace = 0.8

instead? According to matplotlib link , the argument is for changing the height between subplots, not top

.



plt.subplots_adjust(hspace = 0.8)

      

0


source







All Articles