question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to efficiently display a map with CircleMarker() beyond 1000 rows

See original GitHub issue
import 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

fastmarkercluster

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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
jtbakercommented, Nov 5, 2018

@GitAnalyst I reworked your code a little bit and found a few possible issues:

  1. The LayerControl was added before the markers. This was causing a JS console error in Chrome, I guess due to the insertion order in the template.
  2. fill is set to False on your markers, making them hard to see.

with a few modifications, I got these working well using MarkerCluster in the notebook:

import pandas as pd
import folium
from folium.plugins import FastMarkerCluster, MarkerCluster

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')

mc = MarkerCluster(name="Marker Cluster")

for index, row in df.dropna().iterrows():
    popup_text = "{}<br> ALAND: {:,}<br> AWATER: {:,}".format(
                      index,
                      row["ALAND_SQMI"],
                      row["AWATER_SQMI"]
                      )
    folium.CircleMarker(location=[row["INTPTLAT"],row["INTPTLONG"]],
                        radius= 10,
                        color="red",
                        popup=popup_text,
                        fill=True).add_to(mc)

mc.add_to(folium_map)

folium.LayerControl().add_to(folium_map)
folium_map
2reactions
mpickeringcommented, Nov 3, 2018

I had an issue liked this and setting prefer_canvas to True made the performance a lot better.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plotting markers on a map using Pandas & Folium
I am trying to plot a large number (~20,000) of circle markers using Folium. The latitude and longitude data is contained in a...
Read more >
Chapter 4. Visualization with Matplotlib - O'Reilly
The plt.axis() method goes even beyond this, allowing you to do things like ... In visualization of data and results, showing these errors...
Read more >
Documentation - a JavaScript library for interactive maps
Path; Polyline; Polygon; Rectangle; Circle; CircleMarker; SVGOverlay; SVG; Canvas ... Restricts the map view to the given bounds (see the maxBounds option).
Read more >
Read Leaflet Tips and Tricks | Leanpub
Declaring the source for the map tiles. The L.tileLayer function is used to load and display tile layers on the map. L ...
Read more >
Folium 0.12.1 documentation
Make beautiful, interactive maps with Python and Leaflet.js. class folium.folium. ... zoom_control (bool, default True) – Display zoom controls on the map.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found