我正在尝试关注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)
答案 0 :(得分:0)
不确定你是指标记(弹出窗口)还是等值方法本身不起作用?
不推荐使用map1.geo_json()
方法(请参阅How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?)。
相反,请尝试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
方法对我有用,但不知道他们是否修复了等值线图的弹出问题。希望这有帮助!
答案 1 :(得分:0)
正在使用mapObject.choropleth
方法。
folium.GeoJson
: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
将geo_json
替换为GeoJson
,对于fill_color
之类的参数,请在fillColor: <hex_color>
字典kwarg中使用style_function
。