Appending plots to an html using mpld3

23 Views Asked by At

I am trying to save plots to an html using mpld3. After the first run when I run my loop again which runs over a different set of log file (Dataframe) , it is over writing the existing plots and as a results I am not able to save my expected plots to an html.

Basically it should save the plots of both the runs in the same html. I tried using "a" option but of no use

Below is my code:

    with open(output_html, 'w') as file:
        fig, ax = plt.subplots(2, constrained_layout = True)
        for source in iono_corr_group: 
            if source[0][2]==1.0:        #Source-id=1.0 , Output from Correction Handler 
                ax[0].plot(source[1]['iTOW'] / 1000 , source[1]['ionoCorr'] , label=f"{satellite} {source[0][1]}")
                ax[0].set_ylabel("Iono Correction (TECU)")
                ax[0].set_xlabel("Itows (sec)")
                ax[0].set_title('Correction Handler Output - Iono Correction')
            elif source[0][2]==2.0:      #Source-id=2.0 , Output from HPG Filter
                ax[1].plot(source[1]['iTOW'] / 1000 , source[1]['ionoCorr'] , label=f"{satellite} {source[0][1]}")
                ax[1].set_ylabel("Iono Correction (TECU)")
                ax[1].set_xlabel("Itows (sec)")
                ax[1].set_title('HPG Filter Output -Iono Correction')
        mpld3.save_html(fig,file)


        iono_standard_dev=ulsr_msg_df.groupby(['svGnssId','svSvId','sourceId'])
        fig, ax = plt.subplots(2, constrained_layout = True)

        for source in iono_standard_dev: 
            if source[0][2]==1.0:               #Source-id=1.0 , Output from Correction Handler
                ax[0].plot(source[1]['iTOW'] / 1000 , source[1]['svIonoStdev'] , label=f"{satellite} {source[0][1]}")
                ax[0].set_ylabel("Standard deviation (TECU)")
                ax[0].set_xlabel("Itows (sec)")
                ax[0].set_title('Correction Handler Output - Standard Deviation')

            elif source[0][2]==2.0:          #Source-id=2.0 , Output from HPG Filter
                ax[1].plot(source[1]['iTOW'] / 1000 , source[1]['svIonoStdev'] , label=f"{satellite} {source[0][1]}")
                ax[1].set_ylabel("Standard deviation (TECU)")
                ax[1].set_xlabel("Itows (sec)")
                ax[1].set_title('HPG Filter Output - Standard Deviation')
        mpld3.save_html(fig,file)

        new_hpg_groupdf=hpg_df_final.groupby(['svDataGnssId','svDataSvId'])
        fig, ax = plt.subplots(2, constrained_layout = True)
        for hpg_grp in new_hpg_groupdf:
            ax[0].plot(hpg_grp[1]['iTOW'] / 1000 , hpg_grp[1]['ionoEst'] , label=f"{satellite} {hpg_grp[0][1]}")
            ax[0].set_ylabel("Iono Estimation (TECU)")
            ax[0].set_xlabel("Itows (sec)")
            ax[0].set_title('Iono Estimation Output')

            ax[1].plot(hpg_grp[1]['iTOW'] / 1000 , hpg_grp[1]['ionoEstAcc'] , label=f"{satellite} {hpg_grp[0][1]}")
            ax[1].set_ylabel("Iono Accuracy (TECU)")
            ax[1].set_xlabel("Itows (sec)")
            ax[1].set_title('Iono Estimation Accuracy Output')

        mpld3.save_html(fig,file)
        file.close()
0

There are 0 best solutions below