def plot_residual_v_iteration()

in experiments/plot_experiments.py [0:0]


def plot_residual_v_iteration(run_name, formatted_run_name, residuals_df, fig):
    """
    Adds a single trace to the existing figure for experiment 9.2, 9.3 and 9.4
    """
    y = residuals_df[f"{run_name} (median)"]
    y = y[~np.isnan(y)]  # removing pading nans

    n_iterations = len(y)
    iterations = list(range(n_iterations))

    x = iterations.copy()

    y_upper = residuals_df[f"{run_name} (3rd quartile)"].values
    y_lower = residuals_df[f"{run_name} (1st quartile)"].values

    n_max_points = 25
    if n_iterations > n_max_points:
        step = int(n_iterations / n_max_points)
        iter_to_plot = iterations[::step]
        iter_to_plot.append(iterations[-1])
        iter_to_plot = list(dict.fromkeys(iter_to_plot))

        x_to_plot = [x[i] for i in iter_to_plot]
        y_to_plot = y[iter_to_plot]

        y_upper_to_plot = y_upper[iter_to_plot]
        y_lower_to_plot = y_lower[iter_to_plot]
    else:
        x_to_plot = x
        y_to_plot = y

        y_upper_to_plot = y_upper
        y_lower_to_plot = y_lower

    style_key = formatted_run_name.split(" | ")[0]

    current_color = COLOR_DICT[style_key]
    alpha = 0.2  # opacity of the error colored area
    current_color_rgba = ImageColor.getcolor(current_color, "RGB") + (alpha,)

    fig.add_trace(
        go.Scatter(
            x=x_to_plot,
            y=y_to_plot,
            name=formatted_run_name,
            mode="lines+markers",
            line=dict(color=current_color, dash=LINE_DICT[style_key]),
            marker=dict(symbol=SYMBOL_DICT[style_key], size=10),
        )
    )

    fig.add_trace(
        go.Scatter(
            x=x_to_plot,
            y=y_upper_to_plot,
            mode="lines",
            line=dict(width=0),
            marker=dict(color=f"rgba{current_color_rgba}"),
            showlegend=False,
        )
    )

    fig.add_trace(
        go.Scatter(
            x=x_to_plot,
            y=y_lower_to_plot,
            mode="lines",
            line=dict(width=0),
            marker=dict(color=f"rgba{current_color_rgba}"),
            fillcolor=f"rgba{current_color_rgba}",
            fill="tonexty",
            showlegend=False,
        )
    )

    return fig