0

I want to create a figure that consists of 7 plots, 5 boxplots and 2 histograms. The first four boxplots are in the first two rows, followed by a row for the two histograms, and in the last row the last boxplot (figure below). When I try to create this in matplotlib, however, it creates a ton of vertical white space that is unnecessary (red rectangles in the provided images). I want to delete them, i.e. there should be as little as possible top and bottom white space in the plots. I have tried a few things, but nothing seems to work, subplot_adjust (this seems to be the closest to what I want), margin. The link to my python code so far is below, with additional two links on this topic. Thank you for your help!

import numpy as np
import matplotlib.pyplot as plt


dist = np.random.normal(0, 1, 1000)



FONT_SIZE = "xx-large"

fig = plt.figure(figsize=(16, 26))
fig.tight_layout()

gs = fig.add_gridspec(4, 2)


fig_ax1 = fig.add_subplot(gs[0, 0])
fig_ax1.boxplot(
    dist,
    notch=True,
    vert=False)

fig_ax2 = fig.add_subplot(gs[0, 1])
fig_ax2.boxplot(
    dist,
    notch=True,
    vert=False)


fig_ax3 = fig.add_subplot(gs[1, 0])
fig_ax3.boxplot(
    dist,
    notch=True,
    vert=False)


fig_ax4 = fig.add_subplot(gs[1, 1])
fig_ax4.boxplot(
    dist,
    notch=True,
    vert=False)



fig_ax5 = fig.add_subplot(gs[2, 0])
fig_ax5.hist(
    dist,
    bins=60)
fig_ax5.set_xlim(0, 0.02)


fig_ax6 = fig.add_subplot(gs[2, 1])
fig_ax6.hist(
    dist,
    bins=60)



fig_ax7 = fig.add_subplot(gs[3, :])
fig_ax7.boxplot(
    dist,
    notch=True,
    vert=False)

enter image description here

Python code for the Figure

Some links for that issue.

Matplotlib Issue on Margins

Zooming in and out using Axes.margins and the subject of "stickiness"

MacOS
  • 1,149
  • 1
  • 7
  • 14
  • 1
    you should read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Sheldore Jun 05 '20 at 21:13
  • 1
    The normal way to create the axes like `fig, axes = plt.subplots(nrows=4, ncols=2, gridspec_kw={'height_ratios': [1, 1, 3, 1]})`. [docs](https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.subplots.html). Related: [How to use `GridSpec()` with `subplots()`](https://stackoverflow.com/questions/34268742/how-to-use-gridspec-with-subplots) – JohanC Jun 05 '20 at 23:42
  • 1
    Note that StackOverflow doesn't like a pastebin-link to code, because that link can get stale very quickly, leaving the question without contents. Also, github discussions of 7 years ago between the developers aren't usually very helpful for the current version. – JohanC Jun 05 '20 at 23:50
  • @Sheldore Thank you! I see it know. That was too much unnecessary code. I have addressed this in the inline code, which also addresses JohanC remark. – MacOS Jun 06 '20 at 08:41
  • @JohanC Thank you for answer. I will give it try! Regarding the GitHub issue from 7 years: I know it is old, but it seemed to be relevant just a few years ago (scroll down in the issue). – MacOS Jun 06 '20 at 08:43
  • @JohanC Unfortunately, your solution does not seem to work. In my cases, this makes the plots ugly and x-axis description disappears. – MacOS Jun 06 '20 at 09:38
  • Please define "ugly". Did you try the other parameters of [`GridSpec`](https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.gridspec.GridSpec.html#matplotlib.gridspec.GridSpec), e.g. `'hspace'=0.6` to get more spacing between the rows? Please try to experiment with parameters. – JohanC Jun 06 '20 at 10:16

0 Answers0