简体   繁体   English

"如何使用 Folium 将簇标记添加到 Choropleth"

[英]How to add Cluster markers to Choropleth with Folium

I've been working for a while with Choropleth and Cluster marker maps in Folium (which are great).我在 Folium 中使用 Choropleth 和 Cluster 标记图已经有一段时间了(这很棒)。 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:解决方案是先做标记集群,然后用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM