Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
989 views
in Technique[技术] by (71.8m points)

python-3.x - 如何使用mapbox并在国家/地区级别上绘制出一个Choropleth地图?(How to create a choropleth map using mapbox and plotly at a country level?)

I am trying to create a choropleth map using plotly and mapbox similar to the US map https://plot.ly/python/mapbox-county-choropleth/ but of African countries.

(我正在尝试使用plotly和mapbox创建类似于美国地图https://plot.ly/python/mapbox-county-choropleth/的非洲地区的地图。)

I am new to Python and coding period, so I am a bit lost.

(我是Python和编码阶段的新手,所以我有点迷路。)

My geo data json doesn't have the 'id' field like the the Plotly example.

(我的地理数据json没有“ Plotly”示例那样的“ id”字段。)

Do I need to create one?

(我需要创建一个吗?)

Or do I need to just index by df by country?

(还是只需要按国家/地区按df索引?)

But then how do I match up the instances in the json file?

(但是,我该如何匹配json文件中的实例?)

Any help would be appreciated

(任何帮助,将不胜感激)

with open('custom.geo.json') as response:
    africa_geo = json.load(response)
data=pd.read_csv('africa_project.csv')

数据数据框

africa_geo['features'][0]

{'type': 'Feature',
 'properties': {'scalerank': 0,
  'featurecla': 'Admin-0 country',
  'labelrank': 3,
  'sovereignt': 'Burkina Faso',
  'sov_a3': 'BFA',
  'adm0_dif': 0,
  'level': 2,
  'type': 'Sovereign country',
  'admin': 'Burkina Faso',
  'adm0_a3': 'BFA',
  'geou_dif': 0,
  'geounit': 'Burkina Faso',
  'gu_a3': 'BFA',
  'su_dif': 0,
  'subunit': 'Burkina Faso',
  'su_a3': 'BFA',
  'brk_diff': 0,
  'name': 'Burkina Faso',
  'name_long': 'Burkina Faso',
  'brk_a3': 'BFA',
  'brk_name': 'Burkina Faso',
  'brk_group': None,
  'abbrev': 'B.F.',
  'postal': 'BF',
  'formal_en': 'Burkina Faso',
  'formal_fr': None,
  'note_adm0': None,
  'note_brk': None,
  'name_sort': 'Burkina Faso',
  'name_alt': None,
  'mapcolor7': 2,
  'mapcolor8': 1,
  'mapcolor9': 5,
  'mapcolor13': 11,
  'pop_est': 15746232,
  'gdp_md_est': 17820,
  'pop_year': -99,
  'lastcensus': 2006,
  'gdp_year': -99,
  'economy': '7. Least developed region',
  'income_grp': '5. Low income',
  'wikipedia': -99,
  'fips_10_': 'UV',
  'iso_a2': 'BF',
  'iso_a3': 'BFA',
  'iso_n3': '854',
  'un_a3': '854',
  'wb_a2': 'BF',
  'wb_a3': 'BFA',
  'woe_id': 23424978,
  'woe_id_eh': 23424978,
  'woe_note': 'Exact WOE match as country',
  'adm0_a3_is': 'BFA',
  'adm0_a3_us': 'BFA',
  'adm0_a3_un': -99,
  'adm0_a3_wb': -99,
  'continent': 'Africa',
  'region_un': 'Africa',
  'subregion': 'Western Africa',
  'region_wb': 'Sub-Saharan Africa',
  'name_len': 12,
  'long_len': 12,
  'abbrev_len': 4,
  'tiny': -99,
  'homepart': 1,
  'filename': 'BFA.geojson'},
 'geometry': {'type': 'Polygon',
  'coordinates': [[[-0.3976712649999, 15.002134501000072],
    [-0.361471923999943, 15.017740784000113],
    [-0.29917598499992, 15.054741110000064],
    [-0.236699177999867, 15.065618999000035],
    [-0.166987670999902, 15.049676819000098],
    [-0.033404092999916, 14.995933329000096],
    [-0.033197387999962, 14.995933329000096],
    [0.218466838000012, 14.910977275000109],
    [0.22084395400006, 14.888213806000126],
    [0.211852254000121, 14.874803772000078],
    ...]]}}
fig = go.Figure(go.Choroplethmapbox(geojson=africa_geo, locations=data.country_name, z=data.ranking,
                                    colorscale="Viridis"))

fig.update_layout(mapbox_style="dark", mapbox_accesstoken=token,
                  mapbox_zoom=1, mapbox_center = {"lat": 6.6111, "lon": 20.9394})


fig.show()

在此处输入图片说明

  ask by Viktor Avdulov translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Turns out that key is having the "id", a unique identifier called "id".

(事实证明,该密钥具有“ id”,即称为“ id”的唯一标识符。)

If your geo.json doesn't have that it will not work.

(如果您的geo.json没有,它将无法正常工作。)

Then you have to associate the countries with the unique id and make sure your 'locations' is set equal to the df.id

(然后,您必须将国家/地区与唯一ID相关联,并确保您的“位置”设置为等于df.id)

在此处输入图片说明


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...