0

I have followed an online example of using Folium to create a choropleth map with a timeslider, similar to what Aoife Currano has done in this magnificent post on mapping the spread of COVID back in the height of the pandemic.

To me, an obvious enhancement to this map would be adding a tooltip showing the cases of each country for each time period, such that a user can see exactly how many confirmed cases are recorded for each country at the given time period simply by hovering over that country.

I have searched the Folium documentation, but unfortunately, it does not appear possible to add a tooltip to this kind of map. At least, there's no tooltip argument listed in the documentation.

Is there another way of adding a dynamic tooltip to this kind of map? Or would I be better off with another library outside of Folium to accomplish something like this?

1 Answer 1

0

I'm not sure if you are still looking for a solution. But here is a plugin I wrote expanding upon TimeSliderChoropleth. It has tooltips and much more.

https://github.com/germaniuss/folium-timesliderchoropleth

You essentially use it like a GeoJson object in folium, however, the style_function, highlight_function, and tooltips now should point to dictionaries of timestamps.

As an example:

style_function = lambda x: (
    {timestamp0: {'color':...}, ...} if x['id'] == 0
    else {timestamp0: {...}}
)
tooltips = {timestamp1: [Tooltip("Hello World!"), sticky=False), "Plain String"]}

TimeSliderChoroPleth(data, timestamps, style_function=style_function, tooltips=tooltips)

The tooltips variable points to a dictionary that contains entries of (timestamp, list(object)) where the list contains the style for each feature. The style_function returns a dictionary that contains entries of (timestamp, style) given a feature.

A nice way of creating the style function in case you want to use the function with the feature id is to do the following:

# add each feature to style dict
for feature in features:
    # you can skip a timestamp if you want (it will not be displayed)
    styledict[timestamp].append({'color': color, 'opacity': opacity})
# transpose style dict
styledict = [dict(zip(styledict, col)) for col in zip(*styledict.values())]

style_function = lambda x: stylefunction[x['id']]
timestamps = list(styledict[0].keys())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.