I've been working for a while with Choropleth and Cluster marker maps in Folium (which are great). My question is whether it is possible to combine them in one map, which is so that I can see how much one variable affects another. I can get both map types to work individually so no problems there. This is my attempted code to combine the two so far:
import pandas as pd
import folium
from folium.plugins import MarkerCluster
input_filename="input_filename.csv"
df = pd.read_csv(input_filename,encoding='utf8')
geo = 'blah.json'
comparison = 'comparison.csv'
comparison_data = pd.read_csv(comparison)
m = folium.Map(location=[Lat,Lon], zoom_start=12)
folium.Choropleth(
geo_data=geo,
name='choropleth',
data=comparison_data,
columns=['col1','col2'],
key_on='feature.properties.ID',
fill_color='OrRd',
fill_opacity=0.5,
line_opacity=0.5,
legend_name='Blah (%)'
).add_to(m)
folium.LayerControl().add_to(m)
marker_cluster = MarkerCluster().add_to(m)
for row in df.itertuples():
folium.Marker(location=[row.Lat,row.Lon],popup=row.Postcode).add_to(marker_cluster)
m
Ok so I've solved it, really pleased!! The solution was to do the marker cluster first, and then follow-up with the Choropleth:
import pandas as pd
import folium
from folium.plugins import MarkerCluster
m = folium.Map(location=[Lat,Lon], zoom_start=12)
input_filename="input_filename.csv"
df = pd.read_csv(input_filename,encoding='utf8')
geo = 'blah.json'
comparison = 'comparison.csv'
comparison_data = pd.read_csv(comparison)
folium.LayerControl().add_to(m)
marker_cluster = MarkerCluster().add_to(m)
for row in df.itertuples():
folium.Marker(location=[row.Lat,row.Lon],popup=row.Postcode).add_to(marker_cluster)
folium.Choropleth(
geo_data=geo,
name='choropleth',
data=comparison_data,
columns=['col1','col2'],
key_on='feature.properties.ID',
fill_color='OrRd',
fill_opacity=0.5,
line_opacity=0.5,
legend_name='Blah (%)'
).add_to(m)
m
from random import randint
import folium
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
mp = folium.Map(location=[40.6, -73.7], scale = 10)
colors = []
while len(colors) != 50:
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
if rgb_to_hex((r, g, b)) not in colors:
colors.append(rgb_to_hex((r, g, b)))
for j in range(df.shape[0]):
lat = df.iloc[j]['pickup_latitude']
lon = df.iloc[j]['pickup_longitude']
color = colors[int(df.iloc[j]['p_clust'])]
folium.Circle(location=[lat, lon], radius=8, color = color).add_to(mp)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.