Time Slider Choropleth in Folium does not color properly

Viewed 404

I have jupyter notebook code that is supposed to create a time slider choropleth using computed values in a database. However, the coloring is off i.e it doesn't update the colors properly. Here is the relevant code that adds hex color values and dates strings to the groups, and makes a time slider map:


import branca.colormap as cm
import numpy as np
from folium.plugins import HeatMap, TimeSliderChoropleth
from folium import plugins, FeatureGroup

#...other code...

#map values in tot_counts to appropriate hex color values 
min_color, max_color = min(groups["tot_counts"]), max(groups["tot_counts"])
cmap = cm.linear.YlOrRd_09.scale(min_color, max_color)
groups["color"] = groups["tot_counts"].map(cmap)

        
#obtain date values in terms of seconds
groups["dates"] = pd.to_datetime(groups["dates"]).values.astype(float)/ 10 ** 9
groups["dates"] = groups["dates"].astype(int).astype(str)


#create a json object with all keys and values--- keys: country index string, values {'color': X , 'opacity': Y}
style_dict = {}

county_values = groups["county"].unique()
for idx in range(len(county_values)):
    inner_dict = {}
    county = county_values[idx]
    rows = groups[groups["county"]== county]
    for _, row in rows.iterrows(): 
        color = row["color"]
        inner_dict[row["dates"]] = {"color": color, "opacity": 0.4 }
    style_dict[idx] = inner_dict


feature_dict = {}

groups = groups.merge(gpd_counties, left_on="county", right_on="NAME", how="inner")
groups = gpd.GeoDataFrame(groups)


geometries = groups[["geometry"]]

geo_geometries = gpd.GeoDataFrame(geometries)
geo_geometries = geo_geometries.drop_duplicates().reset_index()

starting_location = [36.778259, -119.417931]
slider_map = folium.Map(location= starting_location, zoom_start = 7, max_bounds=True,tiles='cartodbpositron')
TimeSliderChoropleth(
    name="Time slider",
    data=geo_geometries.to_json(),
    styledict=style_dict
).add_to(slider_map)


cmap.add_to(slider_map)
cmap.caption = "Users registering"


Here is what groups and geo_geometries look like:

Groups dataframe Groups dataframe

geometries

The problem can be observed by focusing on San Bernardino county on the bottom right of the map here. According to the table, its tot_count reaches 163, but the slider map doesn't reach this. Any help will be greatly appreciated. Let me know if I can clarify anything or add any missing information.

0 Answers
Related