Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

seaborn/matplotlib sample boxplots

It is pretty much what I want except that I would like to put some more space between the first row of the of the plots and the second row. The distance between the X-axis labels of the first row plots and the title of the second row plots is almost non-existent. I have been playing with the parameters as explained in this thread:

StackOverflow Thread

Here is my relevant 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()
like image 540
Bharat Avatar asked May 15 '17 02:05

Bharat


People also ask

How to adjust spacing between Matplotlib subplots?

How to Adjust Spacing Between Matplotlib Subplots Often you may use subplots to display multiple plots alongside each other in Matplotlib. Unfortunately, these subplots tend to overlap each other by default. The easiest way to resolve this issue is by using the Matplotlib tight_layout () function.

What is the use of tight_layout in Matplotlib?

tight_layout automatically adjusts subplot params so that the subplot (s) fits in to the figure area. This is an experimental feature and may not work for some cases. It only checks the extents of ticklabels, axis labels, and titles.

What is subplots() function in Matplotlib?

Subplots : The subplots () function in pyplot module of matplotlib library is used to create a figure and a set of subplots. Subplots are required when we want to show two or more plots in same figure. Here, first we will see why setting of space is required.

How do I adjust the subplot params when redrawn in Matplotlib?

Note that matplotlib.pyplot.tight_layout () will only adjust the subplot params when it is called. In order to perform this adjustment each time the figure is redrawn, you can call fig.set_tight_layout (True), or, equivalently, set rcParams ["figure.autolayout"] (default: False) to True.


1 Answers

Have you tried adjusting hspace = 0.8 instead? According to matplotlib's reference that's the argument for changing the height between subplots, and not top.

plt.subplots_adjust(hspace = 0.8)
like image 108
Vinícius Figueiredo Avatar answered Oct 17 '22 03:10

Vinícius Figueiredo