2016-02-07 85 views
0

我正在嘗試關注Domino實驗室的博客文章,使用Folium創建交互式犯罪地圖。我發現代碼庫太舊而無法運行Folium的Choropleth地圖標記。儘管Domino平臺上的舊版本似乎可行(2015年),但最新的Ipython筆記本無法正常工作。所以我猜大概Folium改變了一些標記?我試圖找到更新,但我找不到它。有人熟悉這個圖書館嗎?如果是這樣,請給我意見。Folium Choropleth地圖標記不起作用

我下面的代碼:

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

同樣在這裏..你找到解決方案嗎? – user3368526

回答

0

不知道你的意思是標記(彈出窗口)或等值線方法本身不工作?

map1.geo_json()方法已棄用(請參閱here)。

相反,嘗試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方法爲我工作,但不知道他們是否固定在彈出的問題進行等值線圖。希望這可以幫助!