有叶的等值线

Choropleth with folium

我正在尝试使用 folium 创建等值线图。尽管我想创建一张世界地图,但我正在使用以下站点作为指南。

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)

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

执行 'Save link as...'

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

单击 Raw 选项卡,然后当它出现时您可以右键单击然后 'Save as..'

我还要注意,您需要稍微更改一下代码。如果您查看 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')