python - 基于具有国家计数值的数据框的彩色地图
问题描述
我有一个名为 country 的数据框,其中包含国家/地区的 country_code 和频率计数。
国家代码 | 频率 | |
---|---|---|
0 | 我们 | 17600 |
1 | 中国 | 8572 |
2 | 韩国 | 1121 |
3 | J.P | 299 |
4 | 德 | 199 |
我写了一个代码来绘制世界地图上的国家分布:
import plotly.express as px
np.random.seed(12)
gapminder = country
gapminder['counts'] = np.random.uniform(low=100000, high=200000, size=len(gapminder)).tolist()
fig = px.choropleth(gapminder, locations="country_code",
color="frequency",
hover_name="country_code",
color_continuous_scale=px.colors.sequential.Blues)
fig.show()
我得到的结果:
代码有问题,我找不到地图上根本没有显示分布的原因。你能帮我纠正一下吗?谢谢!
解决方案
plotly 中的 choroplethmap 需要指定以哪种格式表示国家名称数据。
locationmode: str 'ISO-3'、'USA-states' 或 'country names' 中的一个 确定用于将条目匹配
locations
到地图上的区域的一组位置。
国名栏是两个字母的缩写,所以需要转换成三个字母的缩写。我不得不手动更正它,因为我有少量数据。
import pandas as pd
import numpy as np
import io
data = '''
country_code frequency
0 USA 17600
1 CHN 8572
2 KOR 1121
3 JPN 299
4 DNK 199
'''
country = pd.read_csv(io.StringIO(data), delim_whitespace=True)
import plotly.express as px
np.random.seed(12)
gapminder = country
gapminder['counts'] = np.random.uniform(low=100000, high=200000, size=len(gapminder)).tolist()
fig = px.choropleth(gapminder, locations="country_code",
locationmode='ISO-3',
color="frequency",
hover_name="country_code",
color_continuous_scale=px.colors.sequential.Blues)
fig.show()
推荐阅读
- javascript - Android webview 显示旧网站内容而不是新网站内容
- r - 查找 0 和 1 的所有组合以及特定数量的 1
- java - 字符串与输入字符串的连接
- javascript - Dynamics CRM 使用 XrmServiceToolkit.Soap 获取全局 OptionSet 数据
- python - 递归函数完成后返回 0。为什么'return 0'不覆盖函数的先前返回值?
- python - Python中非英语语言的拼写检查器
- ios - iOS MPMediaItem:在 iTunes 中设置的选项
- android - 在android的折线图中绘制Json数据
- ios - 在 React Native 中更新应用后维护 Session
- python - 从 SET 数据类型中提取匹配表达式