我正在尝试用大叶草创建我的第一个Choropleth贴图。
我有一个熊猫数据框,我已经成功地使用它填充了大叶圆图,所以我非常有信心数据内容没有问题。下面的函数生成了我的地图就很好了。
def plot_cases(cases_date):
folium_map = folium.Map(location=[40.738, -73.98],
zoom_start=1,
tiles="CartoDB dark_matter",
width='100%')
#add a circle marker for each row in data
for index, row in cases_date.iterrows():
# generate the popup message that is shown on click.
popup_text = "Cases: {}<br> Country: {}<br> Province/State: {}"
popup_text = popup_text.format(row["Cases"], row["Country/Region"], row["Province/State"])
#radius of circle
radius = row["Cases"]/1000
# choose the color of the marker
if row["Cases"] > 0:
color="#E37222" # tangerine
else:
color="#0A8A9F" # teal
# add circle on map
folium.CircleMarker(location=(row["Lat"],
row["Long"]),
radius=radius,
color=color,
popup=popup_text,
fill=True).add_to(folium_map)
return folium_map
现在,当我尝试使用相同的pandas df生成我的弦乐时,我引入的唯一新概念是从git repo抓取世界地图geoJSON文件:
#get geoJSON
req = requests.get('https://raw.githubusercontent.com/python-visualization/folium/master/examples/data/world-countries.json')
geofile = req.json()
#instantiate map and enter choropleth params
m = folium.Map(location=[40.738, -73.98],zoom_start=1,tiles="CartoDB dark_matter",width='100%')
choropleth = folium.Choropleth(
geo_data = geofile,
name='choropleth',
data=choro_data,
columns=['Country/Region', 'Cases'],
key_on='feature.properties.name',
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Cases',
highlight=True,
line_color='black'
).add_to(m)
folium.LayerControl(collapsed=True).add_to(m)
m
上面的choropleth代码生成以下回溯:
TypeError Traceback (most recent call last)
<ipython-input-15-27efeb8f5173> in <module>
12 legend_name='Cases',
13 highlight=True,
---> 14 line_color='black'
15 ).add_to(m)
16
~/cases/map/lib/python3.6/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)
1076 if color_data is not None and key_on is not None:
1077 real_values = np.array(list(color_data.values()))
-> 1078 real_values = real_values[~np.isnan(real_values)]
1079 _, bin_edges = np.histogram(real_values, bins=bins)
1080
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
hpaulj建议json存在问题。您可以在源代码处查看json,但我还从print(geofile)
中提取了一个示例:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Afghanistan"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
61.210817,
35.650072
],
[
62.230651,
35.270664
],
[
62.984662,
35.404041
],
[
63.193538,
35.857166
],
[
63.982896,
36.007957
],
[
64.546479,
36.312073
],
[
64.746105,
37.111818
],
[
65.588948,
37.305217
],
[
65.745631,
37.661164
],
[
66.217385,
37.39379
],
[
66.518607,
37.362784
],
[
67.075782,
37.356144
],
[
67.83,
37.144994
],
[
68.135562,
37.023115
],
[
68.859446,
37.344336
],
[
69.196273,
37.151144
],
[
69.518785,
37.608997
],
[
70.116578,
37.588223
],
[
70.270574,
37.735165
],
[
70.376304,
38.138396
],
[
70.806821,
38.486282
],
[
71.348131,
38.258905
],
[
71.239404,
37.953265
],
[
71.541918,
37.905774
],
[
71.448693,
37.065645
],
[
71.844638,
36.738171
],
[
72.193041,
36.948288
],
[
72.63689,
37.047558
],
[
73.260056,
37.495257
],
[
73.948696,
37.421566
],
[
74.980002,
37.41999
],
[
75.158028,
37.133031
],
[
74.575893,
37.020841
],
[
74.067552,
36.836176
],
[
72.920025,
36.720007
],
[
71.846292,
36.509942
],
[
71.262348,
36.074388
],
[
71.498768,
35.650563
],
[
71.613076,
35.153203
],
[
71.115019,
34.733126
],
[
71.156773,
34.348911
],
[
70.881803,
33.988856
],
[
69.930543,
34.02012
],
[
70.323594,
33.358533
],
[
69.687147,
33.105499
],
[
69.262522,
32.501944
],
[
69.317764,
31.901412
],
[
68.926677,
31.620189
],
[
68.556932,
31.71331
],
[
67.792689,
31.58293
],
[
67.683394,
31.303154
],
[
66.938891,
31.304911
],
[
66.381458,
30.738899
],
[
66.346473,
29.887943
],
[
65.046862,
29.472181
],
[
64.350419,
29.560031
],
[
64.148002,
29.340819
],
[
63.550261,
29.468331
],
[
62.549857,
29.318572
],
[
60.874248,
29.829239
],
[
61.781222,
30.73585
],
[
61.699314,
31.379506
],
[
60.941945,
31.548075
],
[
60.863655,
32.18292
],
[
60.536078,
32.981269
],
[
60.9637,
33.528832
],
[
60.52843,
33.676446
],
[
60.803193,
34.404102
],
[
61.210817,
35.650072
]
]
]
},
"id": "AFG"
}
]
}
答案 0 :(得分:0)
好吧-好像json是红色鲱鱼。我脚本中后面的代码实际上是向data=choro_data
输入错误的数据帧,因此有效地使小叶接收到格式错误的数据集信息并抛出错误。