python - 在 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'
非常感谢您对此的帮助,谢谢!
解决方案
语法发生了变化。
在 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
推荐阅读
- ansible - Ansible 计算远程文件中的行数
- r - 如何在 CatBoost R 包中设置比例和偏差
- ruby - 如何在Ruby中将一个数组映射到两个数组并在满足条件时执行某些功能
- html - 移动设备上的 CSS 拉伸图像
- python - 卡组未完全归还,部分 play() 未执行(Python)
- javascript - 如何在输入“数字”类型时调用方法?
- vb.net - 如何在没有设定列数的情况下将所有行汇总为总列?
- html - 为表中的每一行创建一个 div
- c++ - 如何有效使用调试器研究clang如何构建AST
- python - 如果用户在具有特定角色时离开服务器,则禁止用户 discord.py