0

I need to create 4 plots, where there are two large ones and two stacked smaller ones to the right (the sum is the same as the large plots). Picture attached. enter image description here

So far I managed only to create two separate figures:

fig, axs = plt.subplots(1, 3, figsize=(12, 4))
#plotting
fig, axs_right = plt.subplots(2, 1, figsize=(4, 8))
#plotting

The difficulty is that I need to plot with seaborn and have control over axes to change the way they look.

cheersmate
  • 2,385
  • 4
  • 19
  • 32
stasia_l
  • 47
  • 6
  • see https://stackoverflow.com/questions/2265319/how-to-make-an-axes-occupy-multiple-subplots-with-pyplot – ACarter Jan 26 '23 at 15:37
  • In particular, suggest using subplot_mosaic: https://matplotlib.org/stable/tutorials/provisional/mosaic.html – Jody Klymak Jan 26 '23 at 16:20

2 Answers2

2

This is a relatively simple layout you can achieve with plt.subplot2grid():

grid = (2, 3)

fig = plt.figure(figsize=(8, 6))  # or whatever

ax = plt.subplot2grid(grid, (0, 0), rowspan=2)  # left column plot
...

ax = plt.subplot2grid(grid, (0, 1), rowspan=2)  # middle column plot
...

ax = plt.subplot2grid(grid, (0, 2))  # right column upper plot
...

ax = plt.subplot2grid(grid, (1, 2))  # right column lower plot
...

Result: enter image description here

cheersmate
  • 2,385
  • 4
  • 19
  • 32
0

There are multiple ways of achieving that, it is described in the "Arranging multiple Axes in a Figure" part of the matplotlib documentation.

Below is a basic recopy for your case:

import matplotlib.pyplot as plt

figsize = (7, 4)

# Method 1
fig, axd = plt.subplot_mosaic(
    [
        ["1", "2", "3"],
        ["1", "2", "4"],
    ],
    figsize=figsize,
    layout="constrained",
)


# Method 2
fig = plt.figure(figsize=figsize, layout="constrained")
spec = fig.add_gridspec(ncols=3, nrows=2)

ax1 = fig.add_subplot(spec[:, 0])
ax2 = fig.add_subplot(spec[:, 1])
ax3 = fig.add_subplot(spec[0, 2])
ax4 = fig.add_subplot(spec[1, 2])


# Method 3
fig = plt.figure(figsize=figsize, layout="constrained")
spec0 = fig.add_gridspec(ncols=2, nrows=1, width_ratios=[2, 1])
spec01 = spec0[0].subgridspec(ncols=2, nrows=1)
spec02 = spec0[1].subgridspec(ncols=1, nrows=2)

ax1 = fig.add_subplot(spec01[0])
ax2 = fig.add_subplot(spec01[1])
ax3 = fig.add_subplot(spec02[0])
ax4 = fig.add_subplot(spec02[1])

plt.show()

subplots layout

paime
  • 2,901
  • 1
  • 6
  • 17