【问题标题】:Folium Choropleth map marker is not workingFolium Choropleth 地图标记不起作用
【发布时间】:2016-05-16 22:51:47
【问题描述】:

我正在尝试关注 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)  

【问题讨论】:

  • 这里也一样..你找到解决方案了吗?

标签: python visualization vincent folium


【解决方案1】:

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

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 方法对我有用,但不知道他们是否解决了等值线地图的弹出问题。希望这会有所帮助!

【讨论】:

    【解决方案2】:

    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_json替换为GeoJson

    • 对于像fill_color 这样的args,在style_function 字典kwarg 中使用fillColor: &lt;hex_color&gt;

    【讨论】:

      猜你喜欢
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 2019-05-22
      • 1970-01-01
      • 2019-06-09
      相关资源
      最近更新 更多