A minimal version of my problem is the following plot. How do I scale the right panel to have the same height as the left one?
import matplotlib.pyplot as plt
import numpy as np
rnd = np.random.default_rng(123)
dx1, dy1 = 3, 2
dx2, dy2 = 3, 4
im1 = rnd.random((dy1, dx1))
im2 = rnd.random((dy2, dx2))
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(im1)
ax2.imshow(im2)
I could position both plots by hand, but I need a robust procedure to generate multiple similar plots. I looked at ImageGrid
and GridSpec
but neither seems to support what I need. I would expect subplots
to automatically do what I need, but this is not the case. Is this behaviour by design?
7
The solution consists of using the width_ratios
keyword of plt.subplots
import matplotlib.pyplot as plt
import numpy as np
rnd = np.random.default_rng(123)
dx1, dy1 = 3, 2
dx2, dy2 = 3, 4
im1 = rnd.random((dy1, dx1))
im2 = rnd.random((dy2, dx2))
fig, (ax1, ax2) = plt.subplots(1, 2, width_ratios=[dx1/dy1, dx2/dy2])
ax1.imshow(im1)
ax2.imshow(im2)