pythonmatplotlibplotsubplot

Plotting differently sized subplots in pyplot


I want to plot a figure in pyplot that has 3 subplots, arranged vertically. The aspect ratios of the first one is 1:1, while for the other two it is 2:1. And the heights of each of these plots should be the same. This would mean that the left and right boundaries of the 1st plot are quite far away from the boundaries of the canvas. Could someone please help me with how to go about doing this? I have tried meddling with the hspace command, but that ends up changing the spacing of all the subplots, not just the first one.

I have also tried the following by creating subplots within a subplot, but the wspace doesnt seem to make any difference in the plots:

fig = plt.figure(figsize=(3.385, 2*3))
outer = gridspec.GridSpec(2, 1, wspace=2.0, hspace=0.1, height_ratios=[1,3])
inner1 = gridspec.GridSpecFromSubplotSpec(1, 1,
                    subplot_spec=outer[0], wspace=0.1, hspace=0.1)
inner2= gridspec.GridSpecFromSubplotSpec(2, 1,
                    subplot_spec=outer[1], wspace=0.1, hspace=0.1)



ax1 = plt.Subplot(fig, inner1[0])
(ax2,ax3)= plt.Subplot(fig, inner2[0]), plt.Subplot(fig, inner2[1])

fig.add_subplot(ax1); fig.add_subplot(ax2); fig.add_subplot(ax3)



ax1.plot(x1, y1, "-", color="k" )
ax2.plot(x2, y2, "-")
ax3.plot(x3,y3, "--)

plt.show()

For illustration, I would like a plot that looks something like this:

Could someone please help me with how this could be done?


Solution

  • Create 3 vertically arranged subplots, their heights will by equal be default. Then set the aspect ratios as desired.

    import matplotlib.pyplot as plt
    
    fig, axes = plt.subplots(nrows=3)
    axes[0].set_aspect(1)
    axes[1].set_aspect(.5)
    axes[2].set_aspect(.5)
    

    enter image description here