Subplot individual height

Hi All,

I’m trying to make the first 2 rows of my plot always stay the same height (example here: 200 px and 180 px). However, depending on the number of lines plotting on the 3rd row the total height of the figure should increase. When I do this, the heights of the first two rows change and I wanna avoid that. I’d appreciate if anybody can help.

minimal reproducible code:

def plot_test(n):
    heights = [200,180,n * 15]
    plot_height = sum(heights)
    row_heights = [x / plot_height for x in heights]

    fig = make_subplots(rows=3, cols=1,
                        shared_xaxes=True,
                        vertical_spacing=50/plot_height,subplot_titles=["test 1", "test 2", "test 3"] , row_heights=row_heights)

    fig.add_trace(go.Scatter(x=[0, 1, 2], y=[10, 11, 12]),
                  row=1, col=1)

    fig.add_trace(go.Scatter(x=[2, 3, 4], y=[100, 110, 120]),
                  row=2, col=1)
    
    for i in range(n):
        fig.add_trace(go.Scatter(x=[3, 4, 5], y=[i, i, i],connectgaps=True,
                            marker={
                                "color": "grey",
                                "size": 6,
                                "symbol": "square",
                            },
                            name="",
                            showlegend=False,),
                  row=3, col=1)

    fig.update_layout(height=plot_height, width=600,
                      title_text="Stacked Subplots with Shared X-Axes")
    fig.show()

first example:

plot_test(5)

second example:

plot_test(25)

I’d also want the spacing between each line on the last row stays the same no matter what n is

Hi @rrazagh1 - welcome! I think the discrepancy here is the plot margins, plus I guess the vertical spacing: layout.height is the full height including the top and bottom margins. So give your plot an explicit update_layout(margin={t: top, b: bottom, autoexpand: False}) (so you know what these values are and they don’t get automatically adjusted by a legend or some such) and then calculate the total height based on the desired subplot heights, plus the two inter-subplot spaces and the top and bottom margins.

Thanks a lot, @alexcjohnson! I modified the code below and that fixes the spacing between the individual traces on the 3rd subplot but the main issue is still there. I suspect this is because of the vertical spacing, but not sure how to fix it:

def plot_test(n):
    
    top = 60
    bottom = 60
    vertical_spacing = 50
    
    heights = [200,100,n * 10]
    
    total_vertical_spacing = vertical_spacing * len(heights)
    
    plot_height = sum(heights) + top + bottom + total_vertical_spacing
    row_heights = [x / plot_height for x in heights]
    
    fig = make_subplots(rows=3, cols=1,
                        shared_xaxes=True,
                        vertical_spacing=50/plot_height,subplot_titles=["test 1", "test 2", "test 3"] , row_heights=row_heights)

    fig.add_trace(go.Scatter(x=[0, 1, 2], y=[10, 11, 12]),
                  row=1, col=1)

    fig.add_trace(go.Scatter(x=[2, 3, 4], y=[100, 110, 120]),
                  row=2, col=1)
    
    for i in range(n):
        fig.add_trace(go.Scatter(x=[3, 4, 5], y=[i, i, i],connectgaps=True,
                            marker={
                                "color": "grey",
                                "size": 6,
                                "symbol": "square",
                            },
                            name="",
                            showlegend=False,),
                  row=3, col=1)

    fig.update_layout(height=plot_height, width=600,showlegend=False,
                      title_text="Stacked Subplots with Shared X-Axes")
    return fig