79158612

Date: 2024-11-05 10:15:11
Score: 0.5
Natty:
Report link

This can be achieved with matplotlib GridSpec:

from matplotlib.gridspec import GridSpec

By creating a grid for the subplots you can control the sizes. The layout is a grid with two rows and two columns. One column is reserved for the legends/ labels so that they do not interfere with the size of the main plots.

fig = plt.figure(figsize=(10, 8), dpi=100)
gs = GridSpec(2, 2, width_ratios=[1, 0.3], height_ratios=[1, 1], wspace=0.3, hspace=0.4)

# First plot (Top)
ax1 = fig.add_subplot(gs[0, 0])
my_plot(ax1, x1, [y1_1, y1_2], ["sin", "cos"])

# Second plot (Bottom) with longer labels
ax2 = fig.add_subplot(gs[1, 0])
my_plot(ax2, x1, [y1_1, y1_2], ["long_sine_label", "long_cosine_label"])

Subplots same size, different label lengths

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: EuanG