首页 > 解决方案 > 创建 Choropleth 地图时出错,Json 解码错误

问题描述

我试图在 Python 中使用 Folium 创建一个叶绿素图。但是得到一个错误。数据可以在这里找到

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-42-ab43cadb1d56> in <module>()
      8     fill_opacity=0.7,
      9     line_opacity=0.2,
---> 10     legend_name='Immigration to Canada'
     11 )
     12 

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\folium\folium.py in choropleth(self, geo_data, data, columns, key_on, threshold_scale, fill_color, fill_opacity, line_color, line_weight, line_opacity, name, legend_name, topojson, reset, smooth_factor, highlight)
    325                 style_function=style_function,
    326                 smooth_factor=smooth_factor,
--> 327                 highlight_function=highlight_function if highlight else None)
    328 
    329         self.add_child(geo_json)

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\site-packages\folium\features.py in __init__(self, data, style_function, name, overlay, control, smooth_factor, highlight_function)
    480             else:  # This is a filename
    481                 with open(data) as f:
--> 482                     self.data = json.loads(f.read())
    483         elif data.__class__.__name__ in ['GeoDataFrame', 'GeoSeries']:
    484             self.embed = True

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\json\decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

c:\users\himanshu poddar\appdata\local\programs\python\python36-32\lib\json\decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这是我尝试过的

# download countries geojson file
!wget --quiet https://ibm.box.com/shared/static/cto2qv7nx6yq19logfcissyy4euo8lho.json -O world_countries.json

print('GeoJSON file downloaded!')
world_geo = r'world_countries.json' # geojson file

# create a plain world map
world_map = folium.Map(location=[0, 0], zoom_start=2, tiles='Mapbox Bright')
# generate choropleth map using the total immigration of each country to Canada from 1980 to 2013
world_map.choropleth(
    geo_data=world_geo,
    data=df_can,
    columns=['Country', 'Total'],
    key_on='feature.properties.name',
    fill_color='YlOrRd', 
    fill_opacity=0.7, 
    line_opacity=0.2,
    legend_name='Immigration to Canada'
)

# display map
world_map

请注意,我已经尝试了解决这个问题的所有建议,但没有一个对我有用。

标签: pythonpandasmapschoroplethfolium

解决方案


在我的情况下,错误是choropleth()函数中的 geo_data 没有获得正确的值。


推荐阅读