叶片与叶

时间:2018-12-01 13:11:08

标签: python choropleth folium

我正在尝试使用大叶草创建一个Choropleth贴图。尽管我想创建世界地图,但我仍以以下站点为指南。

https://python-graph-gallery.com/292-choropleth-map-with-folium/

我从这里获得了JSON文件:

https://github.com/python-visualization/folium/blob/master/examples/data/world-countries.json

我的“ test4”数据是这样的:

    Country_0                   Count_0
0   United States of America    18425
1   United Kingdom              3070
2   France                      2705
3   Canada                      1498
4   Japan                       1493
5   Italy                       1471
6   Germany                     1418
7   Russia                      800
8   India                       783
9   Spain                       601
10  Australia                   506
11  Hong Kong                   468
12  South Korea                 457
13  Sweden                      396
14  Finland                     324
15  China                       300
16  Belgium                     299
17  Denmark                     297
18  Brazil                      262
19  Poland                      245
20  Mexico                      236
21  Netherlands                 226
22  Argentina                   211
23  Czech Republic              163
24  Austria                     151
25  Ireland                     135
26  Turkey                      134
27  Greece                      131
28  Norway                      124
29  Hungary                     119
30  Switzerland                 99

我的代码在这里:

import folium
country_count = pd.read_csv('test4.csv')

country_geo = os.path.join('world-countries.json')

m = folium.Map(location=[0, 0], zoom_start=2)
m.choropleth(
 geo_data=country_geo,
 name='choropleth',
 data=country_count,
 columns=['Country_0', 'Count_0'],
 key_on='feature.id',
 fill_color='YlGn',
 fill_opacity=0.7,
 line_opacity=0.2,
 legend_name='Test'
)
folium.LayerControl().add_to(m)

我收到以下错误:

JSONDecodeError: Expecting value: line 7 column 1 (char 6)

1 个答案:

答案 0 :(得分:2)

它是json文件。我假设您右键单击并从https://github.com/python-visualization/folium/tree/master/examples/data

进行了“将链接另存为...”

回到https://github.com/python-visualization/folium/blob/master/examples/data/world-countries.json

单击Raw标签,然后在出现该标签时,右键单击“另存为...”。

我还将注意到,您将需要稍微更改代码。如果您查看json文件,则会看到存储的国家/地区:

enter image description here

与他们使用美国状态json给出的示例不同,美国状态json在功能和ID下具有状态。

因此请注意,这里是提供国家/地区值的功能,属性,名称。因此,请针对key_on属性进行调整:

import pandas as pd
import os
import folium

country_count = pd.read_csv('test4.csv')

country_geo = os.path.join('world-countries.json')

m = folium.Map(location=[0, 0], zoom_start=2)
m.choropleth(
 geo_data=country_geo,
 name='choropleth',
 data=country_count,
 columns=['Country_0', 'Count_0'],
 key_on='feature.properties.name',
 fill_color='YlGn',
 fill_opacity=0.7,
 line_opacity=0.2,
 legend_name='Test'
)
folium.LayerControl().add_to(m)

m.save('#292_folium_chloropleth_country.html')