Filter Folium Map based on marker color

拥有回忆 提交于 2021-02-11 07:57:22

问题


I am mapping markers that have a row called "marker_color" indicating "red", "yellow", and "green" based on other column values. How can I add a filter option to the corner of my map that will allow me to only show one, two, all, or none of the markers based on color? Basically, three clickable radio options to render the three colored markers.

Currently, I am mapping all markers like so from my sales_colored dataframe:

basemap2 = generateBaseMap()

for index, row in sales_colored.iterrows():
    folium.Circle([row['Latitude'], row['Longitude']],
                   radius=200, color=row['marker_color'], fill_color=row['marker_color']).add_to(basemap2)

basemap2

回答1:


If you want to add checkbox for each color, you can use a folium.FeatureGroup(). You need first to collect all unique value in the column sales_colored["marker_color"] and you create one FeatureGroup for each color (I use a dictionnary called features to store the colors). You can create a code like this :

features = {}
for row in pd.unique(sales_colored["marker_color"]):
    features[row] = folium.FeatureGroup(name=row)

for index, row in sales_colored.iterrows():
    circ = folium.Circle([row['Latitude'], row['Longitude']],
                   radius=200, color=row['marker_color'], fill_color=row['marker_color'])
    circ.add_to(features[row['marker_color']])

for row in pd.unique(sales_colored["marker_color"]):
    features[row].add_to(basemap2)

folium.LayerControl().add_to(basemap2)
basemap2


来源:https://stackoverflow.com/questions/64435653/filter-folium-map-based-on-marker-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!