首页 > 解决方案 > 在 Python 上使用 Folium 尝试 Choropleth 地图时出错

问题描述

我一直在使用 Folium Choropleth Maps 绘制一张显示不同旧金山社区犯罪率的地图。

我正在使用的 geojson 文件是: https ://cocl.us/sanfran_geojson我已将其保存为“数据”

我使用的数据来自: https ://cocl.us/sanfran_crime_dataset ,我已将其保存为“cdata”。

我的代码如下;

cdata.rename(columns={'PdDistrict':'Neighbourhood'}, inplace=True)

neighbourhood = cdata.groupby(['Neighbourhood']).size().reset_index(name='Count')

获得:

  Neighbourhood     Count
0   BAYVIEW         14303
1   CENTRAL         17666
2   INGLESIDE       11594
3   MISSION         19503
4   NORTHERN        20100
5   PARK            8699
6   RICHMOND        8922
7   SOUTHERN        28445
8   TARAVAL         11325
9   TENDERLOIN      9942

然后我使用:

sanfran = folium.Map(location=[37.7749, -122.4194], zoom_start = 12)
sanfran.choropleth(
    geo_data=dat,
    name='choropleth',
    data=neighbourhood,
    columns=['Neighbourhood', 'Count'],
    key_on='properties.DISTRICT',
    fill_color='YlOrRd',
    fill_opacity = 0.7,
    line_opacity=0.2,
    legend_name='Crime Rate in San Francisco')

当我运行 sanfran.choropleth 代码时,我收到以下错误:

C:\ProgramData\Anaconda3\lib\site-packages\folium\folium.py:432: FutureWarning: The choropleth  method has been deprecated. Instead use the new Choropleth class, which has the same arguments. See the example notebook 'GeoJSON_and_choropleth' for how to do this.
FutureWarning

然后,如果我只输入“sanfran”并运行我得到的代码:

unhashable type: 'list'

非常感谢您对此的帮助,谢谢!

标签: pythondictionarychoroplethfolium

解决方案


语法发生了变化。

在 folium 上使用大写“C”调用 Choropleth,并使用链“add_to(map)”方法传递您的地图名称,如下所示

folium.Choropleth(all your arguments as the old method).add_to(map)

检查此链接并向下滚动到 Choropleth 地图部分以获取详细信息。 https://python-visualization.github.io/folium/quickstart.html#Getting-Started


推荐阅读