我正在使用以下代码链接到geojson文件,设置threshold_scale并在导入大叶草之后声明Choropleth映射:
san_fran_hoods = r'https://cocl.us/sanfran_geojson'
threshold_scale = np.linspace(df_hoods['Count'].min(),
df_hoods['Count'].max(),
6, dtype=int)
threshold_scale = threshold_scale.tolist() # change the numpy array to a list
threshold_scale[-1] = threshold_scale[-1] + 1 # last value of the list is greater than the maximum
# let Folium determine the scale.
world_map = folium.Map(location=[37.759308, -122.438632], zoom_start=12)
world_map.choropleth(
geo_data = san_fran_hoods,
data = df_hoods,
columns=['Neighborhood', 'Count'],
key_on='features.properties.DISTRICT',
threshold_scale=threshold_scale,
fill_color='YlOrRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Crime Rate in San Francisco')
world_map
在使用choropleth声明运行块之前,我不会收到任何错误。
我不知道如何标记我的legend_name声明。非常感谢您的帮助。
以下是错误消息:
```
/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/folium/folium.py:415: 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
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-20-e01cd86924c1> in <module>
10 fill_opacity=0.7,
11 line_opacity=0.2,
---> 12 legend_name='Crime Rate in San Francisco')
13 world_map
14
/opt/conda/envs/Python-3.7-main/lib/python3.7/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):
/opt/conda/envs/Python-3.7-main/lib/python3.7/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)
1249 style_function=style_function,
1250 smooth_factor=smooth_factor,
-> 1251 highlight_function=highlight_function if highlight else None)
1252
1253 self.add_child(self.geojson)
/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/folium/features.py in __init__(self, data, style_function, highlight_function, name, overlay, control, show, smooth_factor, tooltip, embed, popup)
456 self.convert_to_feature_collection()
457 if self.style:
--> 458 self._validate_function(style_function, 'style_function')
459 self.style_function = style_function
460 self.style_map = {}
/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/folium/features.py in _validate_function(self, func, name)
521 """
522 test_feature = self.data['features'][0]
--> 523 if not callable(func) or not isinstance(func(test_feature), dict):
524 raise ValueError('{} should be a function that accepts items from '
525 'data[\'features\'] and returns a dictionary.'
/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/folium/features.py in style_function(x)
1223
1224 def style_function(x):
-> 1225 color, opacity = color_scale_fun(x)
1226 return {
1227 'weight': line_weight,
/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/folium/features.py in color_scale_fun(x)
1204
1205 def color_scale_fun(x):
-> 1206 key_of_x = get_by_key(x, key_on)
1207 if key_of_x is None:
1208 raise ValueError("key_on `{!r}` not found in GeoJSON.".format(key_on))
/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/folium/features.py in get_by_key(obj, key)
1201 return (obj.get(key, None) if len(key.split('.')) <= 1 else
1202 get_by_key(obj.get(key.split('.')[0], None),
-> 1203 '.'.join(key.split('.')[1:])))
1204
1205 def color_scale_fun(x):
/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/folium/features.py in get_by_key(obj, key)
1200 def get_by_key(obj, key):
1201 return (obj.get(key, None) if len(key.split('.')) <= 1 else
-> 1202 get_by_key(obj.get(key.split('.')[0], None),
1203 '.'.join(key.split('.')[1:])))
1204
AttributeError: 'NoneType' object has no attribute 'get'
```
非常感谢您的帮助。