Plotly Express Choropleth Map Animation loading extremely long

I am making an animated choropleth map of regions in Czechia. When I run it without the animation, purely on one set of the data it takes 7.5 seconds. Here is the code for that.

However when I tried making the animation I had to stop it after 16 hours without a solution. And the HTML file I was saving it in got almost to 1GB in size. Here is the code I use for the animation.

fig = px.choropleth(df_anim,
             locations=REGION_CODE_COL,
             featureidkey=f"properties.{REGION_CODE_COL}",
             geojson=regions_json,
             color=REL_RANGE_TEXT,
             hover_name=REGION_NAME_COL,
             hover_data={
                 REGION_CODE_COL: False, 
                 REGION_NAME_COL: False, 
                 TIME_COL_NAME: False, 
                 REL_COL_NAME: False, 
                 REL_RANGE_TEXT: False,
                 REL_TEXT_COL: True
             },
             color_discrete_map=dict(zip(INTERVAL, COLOR)),
             category_orders={
                  REL_RANGE_TEXT : INTERVAL
              },
             animation_frame=TIME_COL_NAME,
             projection="orthographic"
                   )
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(title_text=MAP_TITLE)
fig.layout.updatemenus[0].buttons[0].args[1]['frame']['duration'] = 1000
fig.layout.updatemenus[0].buttons[0].args[1]['transition']['duration'] = 200
fig.write_html(OUTPUT_HTML_FILE_NAME)
fig.show()

If anyone has any ideas on how to optimize this program I would really appreciate it. The main thing I don’t understand is why REL1506 which is data for year 2015 month 6 takes only 7.5 seconds to load but the animation over 12 months couldn’t finish in 1000minutes (I let it on overnight). I stripped the dataframe and the geojson to only the bare minimum needed for the animation which took the HTML filesize down by about 30% but still wasn’t enough.

Larger regions geojson (1MB), dataframe (2500 rows), animation + HTML file saved (1 minute)
Smaller regions geojson (9MB), dataframe (75000 rows), animation + HTML file saved (couldn’t do it in 1000minutes and the HTML file appeared in my folder after about 6 minutes)

Curious if all the polygon marker line rendering could be expensive on so many small regions (the black lines).

Barring any other ideas, you might try (on a smaller test dataset perhaps), rendering those outlines as transparent instead, by adding the one line of code below before you show the figure.

fig['layout']['geo']['subunitcolor']='rgba(0,0,0,0)'
fig.show()

The add a in rgba here being the alpha channel that controls opacity, effectively making black rgb(0,0,0) into transparent. I haven’t tested this in a while but came across it before in solutions to control the marker line other than using graph objects.

Other than that rendering the legend could be expensive with many animation frames. If suppressing it makes any difference, that could be another area to test options.

1 Like

Thanks for the tip, it sped up the versions with the larger regions which means that it helped but still didn’t solve the issue :frowning:

1 Like