Как дописать эту функцию так, чтобы результирующие графики по индексам idx_i прогнались по всем и сохранились в одной большой png картинке    def get_two_frames(idx_i):
    X_pred_start = dict_X_pred[list_month[idx_i]].copy()
    contracts_list = list(X_pred_start.DEAL_ID.unique())
    # print(len(contracts_list))

    all_first_amount = X_pred_start[X_pred_start.DEAL_ID.isin(contracts_list)]\
                    .groupby('DEAL_ID')['OUTSTANDING_INIT'].first().sum()


    def get_month_pred(X_pred, func_payments='annuity'):
        swap_base = pd.DataFrame()
        bplan_base = pd.DataFrame()
        fix_base = pd.DataFrame()
        annuity_base = pd.DataFrame()

        def get_pretty_base(res_i, name_base):
            res_i.columns = [i + f'_{name_base}' for i in res_i.columns]
            res_i['DEAL_ID'] = df_calc_exmpl['DEAL_ID'].iloc[0]
            res_i['DATE_EOM'] = pd.date_range(start=df_calc_exmpl['DATE_EOM'].iloc[0], periods=res_i.shape[0], freq='M').values
            return res_i

        for i in range(len(contracts_list)):
            df_calc_exmpl = X_pred[X_pred.DEAL_ID == contracts_list[i]].reset_index(drop=True)
            spr_swap = dict(zip(df_calc_exmpl.index, df_calc_exmpl['smm_swap']))
            spr_bplan = dict(zip(df_calc_exmpl.index, df_calc_exmpl['smm_bplan']))
            spr_fix = dict(zip(df_calc_exmpl.index, df_calc_exmpl['smm_fix']))
            spr_annuity = 0

            cred_sum_i = df_calc_exmpl['OUTSTANDING_INIT'].iloc[0]
            cred_rate_i = df_calc_exmpl['PERCSTAV'].iloc[0]
            cred_term_i = df_calc_exmpl.shape[0]

            if func_payments=='annuity':
                res_swap = pd.DataFrame(calculate_credit_life_payments_annuity2(cred_sum_i, cred_rate_i, cred_term_i,spr_swap, 0))
                res_bplan = pd.DataFrame(calculate_credit_life_payments_annuity2(cred_sum_i, cred_rate_i, cred_term_i,spr_bplan, 0))
                res_fix = pd.DataFrame(calculate_credit_life_payments_annuity2(cred_sum_i, cred_rate_i, cred_term_i,spr_fix, 0))
                res_annuity = pd.DataFrame(calculate_credit_life_payments_annuity2(cred_sum_i, cred_rate_i, cred_term_i, spr_annuity, 0))

            elif func_payments=='term':
                res_swap = pd.DataFrame(calculate_credit_life_payments_term(cred_sum_i, cred_rate_i, cred_term_i,spr_swap, 0))
                res_bplan = pd.DataFrame(calculate_credit_life_payments_term(cred_sum_i, cred_rate_i, cred_term_i,spr_bplan, 0))
                res_fix = pd.DataFrame(calculate_credit_life_payments_term(cred_sum_i, cred_rate_i, cred_term_i,spr_fix, 0))
                res_annuity = pd.DataFrame(calculate_credit_life_payments_term(cred_sum_i, cred_rate_i, cred_term_i, spr_annuity, 0))

            res_swap = get_pretty_base(res_swap, 'swap')
            res_bplan = get_pretty_base(res_bplan, 'bplan')
            res_fix = get_pretty_base(res_fix, 'fix')
            res_annuity = get_pretty_base(res_annuity, 'annuity')
            
            swap_base = pd.concat([swap_base, res_swap], axis=0)
            bplan_base = pd.concat([bplan_base, res_bplan], axis=0)
            fix_base = pd.concat([fix_base, res_fix], axis=0)
            annuity_base = pd.concat([annuity_base, res_annuity], axis=0)

        # print(swap_base.shape, bplan_base.shape, fix_base.shape, annuity_base.shape)

        # print(X_pred.shape)
        X_pred = X_pred.merge(swap_base, how='left', on=['DEAL_ID', 'DATE_EOM'])
        X_pred = X_pred.merge(bplan_base, how='left', on=['DEAL_ID', 'DATE_EOM'])
        X_pred = X_pred.merge(fix_base, how='left', on=['DEAL_ID', 'DATE_EOM'])
        X_pred = X_pred.merge(annuity_base, how='left', on=['DEAL_ID', 'DATE_EOM'])
        # print(X_pred.shape)


        X_pred['RUB_OUTSTANDING_EOM_FIRST'] = X_pred.groupby('DEAL_ID')['RUB_OUTSTANDING_EOM'].transform('first')
        X_pred['percent_osz_swap'] = X_pred['ОСЗ_swap'] / X_pred['OUTSTANDING_INIT']
        X_pred['percent_osz_bplan'] = X_pred['ОСЗ_bplan'] / X_pred['OUTSTANDING_INIT']
        X_pred['percent_osz_fix'] = X_pred['ОСЗ_fix'] / X_pred['OUTSTANDING_INIT']
        X_pred['percent_osz_annuity'] = X_pred['ОСЗ_annuity'] / X_pred['OUTSTANDING_INIT']
        return X_pred

    X_pred_annuity = get_month_pred(X_pred_start, 'annuity')
    X_pred_term = get_month_pred(X_pred_start, 'term')
    return X_pred_annuity, X_pred_term


def get_month_plot(X_pred, X_pred2, df_prep, all_first_amount, contracts_list, idx_i, short_plot=True):
    X_pred_annuity, X_pred_term = get_two_frames(idx_i)

    df_check = df_prep[df_prep.DEAL_ID.isin(contracts_list)]
    df_check['OUTSTANDING_PERCENTAGE_W'] = df_check['OUTSTANDING_PERCENTAGE'] * df_check['OUTSTANDING_INIT']
    osz_perc_w_fact = df_check.groupby('DATE_EOM')['OUTSTANDING_PERCENTAGE_W'].sum()\
            .div(all_first_amount, axis=0)


    # cols_weight = ['percent_osz_annuity', 'percent_osz_swap', 'percent_osz_bplan', 'percent_osz_fix']
    cols_weight = ['percent_osz_annuity', 'percent_osz_swap']
    col_w = 'OUTSTANDING_INIT'
    osz_perc_w = X_pred[cols_weight]\
        .mul(X_pred[col_w], axis=0)\
        .join(X_pred['DATE_EOM'])\
        .groupby('DATE_EOM')[cols_weight].sum()\
        .div(all_first_amount, axis=0)
    

    cols_weight2 = ['percent_osz_swap']
    osz_perc_w2 = X_pred2[cols_weight2]\
        .mul(X_pred2[col_w], axis=0)\
        .join(X_pred2['DATE_EOM'])\
        .groupby('DATE_EOM')[cols_weight2].sum()\
        .div(all_first_amount, axis=0)

    if short_plot:
        osz_perc_w = osz_perc_w.iloc[:len(osz_perc_w_fact), :]
        osz_perc_w2 = osz_perc_w2.iloc[:len(osz_perc_w_fact), :]

        
    plt.figure(figsize=(8, 6), dpi=100)
    plt.plot(osz_perc_w_fact.index, osz_perc_w_fact.values, linewidth= 1, label='Fact Outstanding')
    plt.plot(osz_perc_w.index, osz_perc_w['percent_osz_annuity'].values, linewidth= 1, label='Modeled Outstanding (annuity)')
    plt.plot(osz_perc_w.index, osz_perc_w['percent_osz_swap'].values, linewidth= 1, label='Modeled Outstanding (reduce month payment)')
    plt.plot(osz_perc_w2.index, osz_perc_w2.values, linewidth= 1, label='Modeled Outstanding (reduce term)')
    plt.legend()
    plt.title(f'Month = {df_check.VINTAGE_MONTH_NUM.values[0]}, clients - {int(len(contracts_list))}')
    plt.xlabel("DATE_EOM", fontsize=10)
    plt.xticks(rotation=45)
    plt.ylabel("Percentage", fontsize=10)
    plt.grid(True, linestyle='-', alpha=0.7)
    plt.show()

get_month_plot(X_pred_annuity, X_pred_term, df_prep, all_first_amount, contracts_list, 0)
# get_month_plot(X_pred_annuity, X_pred_term, df_prep, all_first_amount, contracts_list, False)