0

我正在尝试关注 Domino 实验室的博客文章,使用 Folium 创建交互式犯罪地图。而且我发现代码库太旧,无法运行 Folium 的 Choropleth 地图标记。尽管 Domino 平台上的旧版本似乎可以工作(2015 年),但最新的 Ipython 笔记本不起作用。所以我猜Folium改变了标记?我试图找到更新,但我找不到它。有人熟悉这个库吗?如果是这样,请给我建议。

我的代码如下:

from IPython.display import HTML

def display(m, height=500):
    """Takes a folium instance and embed HTML."""
    m._build_map()
    srcdoc = m.HTML.replace('"', '"')
    embed = HTML('<iframe srcdoc="{0}" '
                 'style="width: 100%; height: {1}px; '
                 'border: none"></iframe>'.format(srcdoc, height))
    return embed

import folium  
import pandas as pd

SF_COORDINATES = (37.76, -122.45)  
crimedata = pd.read_csv('data/SFPD_Incidents_-_Current_Year__2015_.csv')

#for speed purposes
MAX_RECORDS = 1000

#create empty map zoomed in on San Francisco
map = folium.Map(location=SF_COORDINATES, zoom_start=12)

#add a marker for every record in the filtered data, use a clustered view
for each in crimedata[0:MAX_RECORDS].iterrows():  
    map.simple_marker(
        location = [each[1]['Y'],each[1]['X']], 
        clustered_marker = True)

display(map)  

#definition of the boundaries in the map
district_geo = r'data/sfpddistricts.json'

#calculating total number of incidents per district
crimedata2 = pd.DataFrame(crimedata['PdDistrict'].value_counts().astype(float))  
crimedata2.to_json('data/crimeagg.json')  
crimedata2 = crimedata2.reset_index()  
crimedata2.columns = ['District', 'Number']

#creation of the choropleth
map1 = folium.Map(location=SF_COORDINATES, zoom_start=12)  
map1.geo_json(geo_path = district_geo,  
              data_out = 'data/crimeagg.json', 
              data = crimedata2,
              columns = ['District', 'Number'],
              key_on = 'feature.properties.DISTRICT',
              fill_color = 'YlOrRd', 
              fill_opacity = 0.7, 
              line_opacity = 0.2,
              legend_name = 'Number of incidents per district')

display(map1)  
4

2 回答 2

0

不确定您的意思是标记(弹出窗口)还是 choropleth 方法本身不起作用?

map1.geo_json()方法已弃用(参见此处)。

相反,尝试map1.choropleth(geo_path = district_geo,
data_out = 'data/crimeagg.json', data = crimedata2, columns = ['District', 'Number'], key_on = 'feature.properties.DISTRICT', fill_color = 'YlOrRd', fill_opacity = 0.7, line_opacity = 0.2, legend_name = 'Number of incidents per district')

map.choropleth方法对我有用,但不知道他们是否修复了等值线地图的弹出问题。希望这可以帮助!

于 2016-05-04T19:52:04.143 回答
0

mapObject.choropleth方法正在被贬低。

folium.GeoJson是根据此 github 问题建议的方法:https ://github.com/python-visualization/folium/issues/589

该问题中的评论链接到此示例,该示例显示了如何构建 choropleth: http ://nbviewer.jupyter.org/github/python-visualization/folium/blob/master/examples/GeoJSON_and_choropleth.ipynb?flush_cache=true

TLDR

  • 替换geo_jsonGeoJson

  • 对于 args fill_colorfillColor: <hex_color>请在style_function字典 kwarg 中使用。

于 2018-08-26T16:15:12.517 回答