【问题标题】:Error when attempting Choropleth map using Folium on Python在 Python 上使用 Folium 尝试 Choropleth 地图时出错
【发布时间】:2019-06-09 09:36:03
【问题描述】:

我一直在使用 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'

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

【问题讨论】:

  • 你能提供完整的堆栈跟踪吗? “unhashable type: 'list'”可能是由于几个原因。
  • 您收到的警告告诉您应该使用类Chorophlet 而不是方法chorophlet。检查此link 以了解如何执行此操作:转到“使用 Chorophlet 类”部分(单元格 In [15]

标签: python dictionary choropleth folium


【解决方案1】:

如果你想运行和创建地图,你应该遵循 folium 的新语法。 你的代码应该是这样的:

sanfran = folium.Map(location=[37.7749, -122.4194], zoom_start=12)
folium.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').add_to(sanfran)

sanfran.save('outfile.html')

【讨论】:

    【解决方案2】:

    语法发生了变化。

    在 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

    【讨论】:

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