首页 > 解决方案 > 为什么在 Folium.choropleth 中使用 bins 参数时会发生 KeyError?

问题描述

我正在研究 Folium choropleth。

当我在bins参数上输入 11 个值时,KeyError会发生。

当我输入低于 10 个值时,不会发生错误。

我的代码如下:

map.choropleth(geo_data = json.load,
          data = dataframe,
          columns=[column_names],
          fill_color = 'YlGnBu',
          key_on = 'features.id',
          bins = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50])

错误如下:

KeyError                                  Traceback (most recent call last)
<ipython-input-13-2a6160fee268> in <module>
      4               fill_color = 'YlGnBu',
      5               key_on = 'properties.name',
----> 6               bins = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50])
      7 
      8 

~\anaconda3\lib\site-packages\folium\folium.py in choropleth(self, *args, **kwargs)
    416         )
    417         from folium.features import Choropleth
--> 418         self.add_child(Choropleth(*args, **kwargs))
    419 
    420     def keep_in_front(self, *args):

~\anaconda3\lib\site-packages\folium\features.py in __init__(self, geo_data, data, columns, key_on, bins, fill_color, nan_fill_color, fill_opacity, nan_fill_opacity, line_color, line_weight, line_opacity, name, legend_name, overlay, control, show, topojson, smooth_factor, highlight, **kwargs)
   1181             # We add the colorscale
   1182             nb_bins = len(bin_edges) - 1
-> 1183             color_range = color_brewer(fill_color, n=nb_bins)
   1184             self.color_scale = StepColormap(
   1185                 color_range,

~\anaconda3\lib\site-packages\branca\utilities.py in color_brewer(color_code, n)
    150     if not explicit_scheme:
    151         # Check to make sure that it is not a qualitative scheme.
--> 152         if scheme_info[base_code] == 'Qualitative':
    153             matching_quals = []
    154             for key in schemes:

KeyError: 'YlGnBu'

为了找出原因,我用谷歌搜索并找到了下面的问题。

在 Choropleth #1254 中使用“bins”参数后出现 KeyError

有评论说KeyError出现的原因是因为' color YlGn has 9 shades '。

真的吗?我在哪里可以查看颜色的阴影信息?

标签: pythonfoliumchoropleth

解决方案


推荐阅读