How to efficiently display a map with CircleMarker() beyond 1000 rows
See original GitHub issueimport pandas as pd
import folium
from folium.plugins import FastMarkerCluster
file_url = 'http://www2.census.gov/geo/docs/maps-data/data/gazetteer/2016_Gazetteer/2016_Gaz_zcta_national.zip'
#Pandas usually infers zips are numerics, but we lose our leading zeroes so let's go with the object dtype
df = pd.read_csv(file_url, sep='\t', dtype={'GEOID' : object})
df.columns = df.columns.str.strip() #some column names have some padding
df = df.sample(1000)
folium_map = folium.Map(location=[38, -97],
zoom_start=4.4,
tiles='CartoDB dark_matter')
# These two lines should create FastMarkerClusters
FastMarkerCluster(data=list(zip(df['INTPTLAT'].values, df['INTPTLONG'].values))).add_to(folium_map)
folium.LayerControl().add_to(folium_map)
for index, row in df.iterrows():
# generate the popup message that is shown on click.
popup_text = "{}<br> ALAND: {:,}<br> AWATER: {:,}"
popup_text = popup_text.format(
index,
row["ALAND_SQMI"],
row["AWATER_SQMI"]
)
folium.CircleMarker(location=(row["INTPTLAT"],
row["INTPTLONG"]),
radius= row['ALAND_SQMI']/100,
color="#007849",
popup=popup_text,
fill=False).add_to(folium_map)
folium_map
Problem description
I use CircleMarker() to describe my data by varying radius and color variables (I presented here a more primitive example where only radius varies). The problem is that the map with CircleMarker() doesn’t show up (in Jupyter Notebook) when data has more than 1000 lines. I can save maps with more data points to an HTML file, but it is SLOW to navigate.
Expected Output
So to solve this I wanted to use marker clusters. I tried both MarkerCluster() and FastMarkerCluster(). My understanding is that MarkerCluster() provides with enough flexibility, but for me the plot doesn’t show up when there are more rows in data than 1000. In the example here I tried to use FastMarkerCluster(). But how to use it so that my data points would retain all the features (radius, color, popup) and would be hidden until you zoom in? And I assume this should solve the issue of not displaying the map with larger data sets.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:9 (4 by maintainers)
Top GitHub Comments
@GitAnalyst I reworked your code a little bit and found a few possible issues:
fill
is set toFalse
on your markers, making them hard to see.with a few modifications, I got these working well using
MarkerCluster
in the notebook:I had an issue liked this and setting
prefer_canvas
toTrue
made the performance a lot better.