Search code examples
pythonplotlyfolium

How do I achieve adding "count" on the map in Folium?


I'm working on plotting orders count over US interactive map using Folium. I'm able to plot all the instances but i can't get the map to show the "orders" weight on map, but only as pop up, which isn't help

import pandas as pd
import folium
from folium import Choropleth, Circle, Marker
from folium import plugins
from folium.plugins import HeatMap, MarkerCluster

# Load the data
orderlist=pd.read_csv("/content/orderlist.csv")

# Drop rows with missing locations
orderlist.dropna(subset=['Lat', 'Long'], inplace=True)
print(orderlist)
state_id orders Lat Long
0 AL 50 32.8067 -86.7911
1 AK 13 61.3707 -152.404
4 AR 44 34.9697 -92.3731
5 AZ 70 33.7298 -111.431
6 CA 388 36.1162 -119.682
7 CO 70 39.0598 -105.311
8 CT 41 41.5978 -72.7554
9 DC 5 38.8974 -77.0268
10 DE 19 39.3185 -75.5071
11 FL 240 27.7663 -81.6868
12 GA 145 33.0406 -83.6431
14 HI 19 21.0943 -157.498
15 IA 60 42.0115 -93.2105
16 ID 25 44.2405 -114.479
17 IL 157 40.3495 -88.9861
18 IN 96 39.8494 -86.2583
19 KS 35 38.5266 -96.7265
20 KY 74 37.6681 -84.6701
21 LA 66 31.1695 -91.8678
22 MA 93 42.2302 -71.5301
23 MD 118 39.0639 -76.8021
24 ME 13 44.6939 -69.3819
25 MI 150 43.3266 -84.5361
26 MN 67 45.6945 -93.9002
27 MO 108 38.4561 -92.2884
28 MS 36 32.7416 -89.6787
29 MT 20 46.9219 -110.454
30 NC 150 35.6301 -79.8064
31 ND 15 47.5289 -99.784
32 NE 29 41.1254 -98.2681
33 NH 27 43.4525 -71.5639
34 NJ 161 40.2989 -74.521
35 NM 6 34.8405 -106.248
36 NV 29 38.3135 -117.055
37 NY 288 42.1657 -74.9481
38 OH 170 40.3888 -82.7649
39 OK 51 35.5653 -96.9289
40 OR 57 44.572 -122.071
41 PA 200 40.5908 -77.2098
43 RI 5 41.6809 -71.5118
44 SC 70 33.8569 -80.945
45 SD 13 44.2998 -99.4388
46 TN 100 35.7478 -86.6923
47 TX 325 31.0545 -97.5635
48 UT 41 40.15 -111.862
49 VA 121 37.7693 -78.17
50 VT 15 44.0459 -72.7107
51 WA 97 47.4009 -121.49
52 WI 74 44.2685 -89.6165
53 WV 28 38.4912 -80.9545
54 WY 11 42.756 -107.302
#Map Style 2
map2 = folium.Map([48, -102], zoom_start=4,width="%100",height="%100")
locations = list(zip(orderlist.Lat, orderlist.Long))
cluster = plugins.MarkerCluster(locations=locations,                   
               popups=orderlist["orders"].tolist())  
map2.add_child(cluster)
map2

**Here comes the question: How to show the order number on Map, instead of a pop up? (Using MarkerCluster) **

Output sample: Couldn't post the required output sample (You need at least 10 reputation to post images.), Please see it on colab instead

HEY!! You don't need to copy/paste to work on this code, You can simply play/edit it on colab then post the answer https://colab.research.google.com/drive/1Fs2Xwoc3DHAZe283X3keVMbIQpRAe3eH?usp=sharing


Solution

  • If you want to display text strings as annotations on the map in folium, you can do so by using icons to embed text information in the html.

    import folium
    from folium.features import DivIcon
    
    map2 = folium.Map([45, -102], zoom_start=4, width="%100", height="%100")
    
    for lat,lon,order,state in zip(df.Lat, df.Long, df.orders, df.state_id):
        folium.Marker(location=[lat,lon],
                      icon=DivIcon(
                          icon_size=(32,32),
                          icon_anchor=(0,0),
                          html=f'<div style="font-size:14pt;color:green">{order}</div>'
                      )
                     ).add_to(map2)
    map2
    

    enter image description here