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)