Help request for figure and plot heights

Hello, I’m trying to produce some plots for an open-source course so I need some standardization.

My plots have three “areas”: the title which has a predefined fixed height, the plot area which has a predefined fixed height and the legend which has a variable number of items, a predefined size of font, and a distance from the label of the x-axis which i want to be fixed and predefined.

I tried for many hours but I wasn’t able to find a combination of values for margin and legend position to have those requirements fulfilled.

Can someone help me?


Hi @antomon, welcome to the Poltly community!

Could you share the code you have so far and maybe a screenshot of the plot it produces?

Code and screenshots are below.

I tried with some “heuristics” but the problem is that I wasn’t able to calculate the correct distances even if the only element that is dynamic is the number of legend items. Every other element is fixed, even the top border which contains the title. You can see the mess when legend entries range form a few to a dozen. Legend is under the plot.

practically speaking: the formula I’m trying to implement is

figure height = title height + plot area height + padding plot area-legend + legend height
legend height = number of items * font size * 1.2 (to take into account the spacing between consecutive rows)
title height, plot area height, padding plot area-legend are all predefined so are not variable

    if weights_list and isinstance(weights_list[0], list):
        extra_height = 10 + LEGEND_FONT_SIZE * (len(weights_list) + 1) # legend height: 10 is a padding and (len(weights_list) + 1) is the number of legend items
        fig_height = (PLOT_AREA_HEIGHT + TITLE_HEIGHT + extra_height) * 1.12
        margin_dict = {'t': TITLE_HEIGHT, 'b': extra_height, 'r': 40}

    fig.update_layout(title=dict(text=title), margin=margin_dict,
                      xaxis_title=xaxis_title, yaxis_title=yaxis_title,
                      autosize=False, width=FIGURE_AREA_WIDTH, height=fig_height,
                      font=dict(family=FONT_FAMILY, size=FONT_SIZE, color=FONT_COLOR),
                      xaxis_range=[x_min_axis, x_max_axis],
                      yaxis_range=[y_min_axis, y_max_axis],
                      legend=dict(x=-0.05, y=-0.1, xanchor='left', yanchor='top',
                                  font=dict(family=FONT_FAMILY, size=LEGEND_FONT_SIZE, color=LEGEND_FONT_COLOR)))