Is there a way to plot many markers in Folium?

2020-08-01 05:04发布

问题:

I am trying to use Folium for geographical information reading from a pandas dataframe. The code I have is this one:

import folium
from folium import plugins
import pandas as pd

...operations on dataframe df...

map_1 = folium.Map(location=[51.5073219, -0.1276474],
                   zoom_start=12,
                   tiles='Stamen Terrain')
markerCluster = folium.plugins.MarkerCluster().add_to(map_1)
lat=0.
lon=0.
for index,row in df.iterrows():
    lat=row['lat]
    lon=row['lon']
    folium.Marker([lat, lon], popup=row['name']).add_to(markerCluster)
map_1

df is a dataframe with longitude, latitude and name information. Longitude and latitude are float. I am using jupyter notebook and the map does not appear. Just a white empty box. I have also tried to save the map:

map_1.save(outfile='map_1.html')

but also opening the file doesn't work (using Chrome, Firefox or Explorer). I have tried to reduce the number of markers displayed and below 300 markers the code works and the map is correctly displayed. If there are more than 300 Markers the map returns to be be blank. The size of the file is below 5 MB and should be processed correctly by Chrome.

Is there a way around it (I have more than 2000 entries in the dataframe and I would like to plot them all)? Or to set the maximum number of markers shown in folium?

Thanks

回答1:

This might be too late but I stumbled upon the same problem and found a solution that worked for me without having to remove the popups so I figured if anybody has the same issue they can try it out. Try replacing popup=row['name'] with popup=folium.PopUp(row['name'], parse_html=True) and see whether it works. You can read more about it here https://github.com/python-visualization/folium/issues/726