基于联合国国家指标 2025数据(UN Countries Metrics 2025: HDI, GDP, POP, AREA)的综合可视化分析
摘要:本研究聚焦于多国多维度指标数据的分析与可视化呈现。通过对原始数据进行系统的预处理,运用多种可视化方法,提供了直观数据视角。
仅作技术讨论,如果错误之处,还望指出
关键词:多国指标;数据预处理;可视化分析
一、数据集
我用夸克网盘给你分享了「UN Countries Metrics 2025: HDI, GDP, POP, AREA」,链接:https://pan.quark.cn/s/f44916f48d9d
第 1 列:按字母顺序排列的所有联合国会员国(包括观察员国)的名称。
第 2 列:每个国家的人口。
第 3 列:每个国家的名义 GDP。(美元)
第 4 列:该国的人均名义 GDP。(美元)
第 5 列:每个国家的人均 GDP 购买力平价。(美元)
第 6 列:每个国家的人类发展指数(从 0-1 测量的 HDI)。
第 7 列:基尼系数
第 8 列:每个国家的面积(以平方公里为单位)。
- country_name 国家名称(按字母顺序排列)。
- Population (in millions)人口数量(单位:百万,如 41.45 = 4145万人)。
- Nominal GDP (in USD)名义GDP(单位:美元,当前市场价格计算的经济总量)。
- Nominal GDP Per capita (in USD) 人均GDP(单位:美元,GDP除以人口)。
- GDP Per capita PPP (in USD)人均GDP(购买力平价调整,单位:美元,更真实反映生活水平)。
- Human Development Index (HDI)人类发展指数(0-1,越高越发达,综合健康、教育、经济)
- GINI 基尼系数(0-1,越高收入越不平等)。
- AREA 国土面积(单位:平方公里)。
二、数据加载与预处理
2.1 数据加载
利用pandas
库的read_csv
函数,从指定路径'countries_metric - Sheet1.csv'
读取数据集,并将其存储为DataFrame
格式,代码如下:
file_path = 'countries_metric - Sheet1.csv'
df = pd.read_csv(file_path)
2.2 数据预处理
为确保数据的一致性和可用性,对数据集中各列数据进行类型转换和清理操作。
- 人口数据处理:通过
str.replace
方法去除'Population (in millions)'
列中的逗号,并将数据类型转换为浮点数,代码如下:
df['Population (in millions)'] = df['Population (in millions)'].str.replace(',', '').astype(float)
- GDP相关数据处理:定义
clean_currency
函数,用于处理'Nominal Gross Domestic Product (in USD)'
列中的货币符号以及不同量级的表示字符串(如' trillion'
、' billion'
、' million'
),将其转换为科学计数法形式后尝试转换为浮点数,无法转换的值设为NaN
。同时,对'Nominal GDP Per capita (in USD)'
和'GDP Per capita PPP (in USD)'
列,直接去除逗号并转换为浮点数类型。
def clean_currency(value):if isinstance(value, str):value = value.replace('$', '').replace(' trillion', 'e12').replace(' billion', 'e9').replace(' million', 'e6')try:return float(value)except:return np.nanreturn value
df['Nominal Gross Domestic Product (in USD)'] = df['Nominal Gross Domestic Product (in USD)'].apply(clean_currency)
df['Nominal GDP Per capita (in USD)'] = df['Nominal GDP Per capita (in USD)'].str.replace(',', '').astype(float)
df['GDP Per capita PPP (in USD)'] = df['GDP Per capita PPP (in USD)'].str.replace(',', '').astype(float)
- HDI和GINI数据处理:使用
pd.to_numeric
函数将'Human Development Index (HDI)'
和'GINI'
列转换为数值类型,对于无法转换的值(如非数字字符),设置errors='coerce'
将其转换为NaN
。
df['Human Development Index (HDI)'] = pd.to_numeric(df['Human Development Index (HDI)'], errors='coerce')
df['GINI'] = pd.to_numeric(df['GINI'], errors='coerce')
- 面积数据处理:对
'AREA (in Sq km)'
列,通过str.replace
去除逗号,并将数据类型转换为浮点数。
df['AREA (in Sq km)'] = df['AREA (in Sq km)'].str.replace(',', '').astype(float)
三、可视化分析
3.1 人口与GDP的关系
运用seaborn
库的scatterplot
函数绘制散点图,以探究国家人口与名义GDP之间的关系。通过hue
参数依据'Human Development Index (HDI)'
对散点进行着色,使用size
参数根据'Nominal GDP Per capita (in USD)'
调整气泡大小,设置气泡大小范围为(20, 200)
,透明度为0.7
,并对x轴和y轴采用对数尺度,使数据分布特征更清晰地展现。
plt.figure(figsize=(12, 8))
sns.scatterplot(data=df, x='Population (in millions)', y='Nominal Gross Domestic Product (in USD)',hue='Human Development Index (HDI)', size='Nominal GDP Per capita (in USD)',sizes=(20, 200), alpha=0.7, palette='viridis',hue_norm=(df['Human Development Index (HDI)'].min(), df['Human Development Index (HDI)'].max()))
plt.xscale('log')
plt.yscale('log')
plt.title('国家人口与GDP关系图 (按HDI着色,气泡大小表示人均GDP)')
plt.xlabel('人口')
plt.ylabel('名义GDP (美元)')
plt.grid(True, which="both", ls="--")
plt.tight_layout()
#plt.savefig('population_gdp_relationship.png', dpi=300)
plt.show()
总量增长依赖人口基数,但高质量发展依赖人均水平(气泡大小)和发展质量(HDI颜色);
3.2 HDI与GDP per capita的关系
同样利用scatterplot
绘制散点图,分析人类发展指数(HDI)与人均GDP之间的关系。以'GINI'
作为着色依据,通过size
参数依据'Population (in millions)'
调整气泡大小,x轴采用对数尺度,以便更好地观察两者之间的关系。
plt.figure(figsize=(12, 8))
sns.scatterplot(data=df, x='Nominal GDP Per capita (in USD)', y='Human Development Index (HDI)',hue='GINI', size='Population (in millions)',sizes=(20, 200), alpha=0.7, palette='coolwarm',hue_norm=(df['GINI'].min(), df['GINI'].max()))
plt.xscale('log')
plt.title('HDI与人均GDP关系图 (按GINI着色,气泡大小表示人口)')
plt.xlabel('人均GDP (美元)')
plt.ylabel('人类发展指数 (HDI)')
plt.grid(True, which="both", ls="--")
plt.tight_layout()
#plt.savefig('hdi_gdp_per_capita.png', dpi=300)
plt.show()
- 人均GDP增长推动HDI提升,但高收入阶段边际效应减弱。
- GINI系数高的国家,同等经济水平下HDI更低。
- 中等收入大国多处于“经济提速但HDI滞后”阶段。
3.3 各国HDI排名前20
对数据集按'Human Development Index (HDI)'
进行降序排序,选取排名前20的国家。使用seaborn
的barplot
函数绘制条形图,展示这些国家的HDI情况。通过hue
参数根据国家名称区分颜色,采用'viridis'
调色板,使图表更具可读性。
top_hdi = df.sort_values('Human Development Index (HDI)', ascending=False).head(20)
plt.figure(figsize=(12, 8))
sns.barplot(data=top_hdi, x='Human Development Index (HDI)', y='country_name', hue='country_name', palette='viridis')
plt.title('HDI排名前20的国家')
plt.xlabel('人类发展指数 (HDI)')
plt.ylabel('国家')
plt.grid(True, which="both", ls="--", axis='x')
plt.tight_layout()
plt.show()
3.4 各国GINI系数排名前20 (最高不平等)
按'GINI'
对数据集进行降序排序,选取前20个国家。利用barplot
绘制条形图,展示这些国家的GINI系数,反映其收入不平等程度。通过hue
参数根据国家名称区分颜色,使用'rocket'
调色板,同时添加网格线以增强可读性。
top_gini = df.sort_values('GINI', ascending=False).head(20)
plt.figure(figsize=(12, 8))
sns.barplot(data=top_gini, x='GINI', y='country_name',hue='country_name', palette='rocket')
plt.title('GINI系数最高的前20个国家 (收入不平等程度)')
plt.xlabel('基尼系数')
plt.ylabel('国家')
plt.grid(True, which="both", ls="--", axis='x')
plt.tight_layout()
plt.show()
3.5 GDP per capita PPP与HDI的关系
借助scatterplot
绘制散点图,研究人均GDP PPP与HDI之间的关系。通过hue
参数依据'Population (in millions)'
对散点进行着色,size
参数根据'Nominal Gross Domestic Product (in USD)'
调整气泡大小,x轴采用对数尺度,以便清晰展示两者关系。
plt.figure(figsize=(12, 8))
sns.scatterplot(data=df, x='GDP Per capita PPP (in USD)', y='Human Development Index (HDI)',hue='Population (in millions)', size='Nominal Gross Domestic Product (in USD)',sizes=(20, 200), alpha=0.7)
plt.xscale('log')
plt.title('人均GDP PPP与HDI关系图 (按人口着色,气泡大小表示名义GDP)')
plt.xlabel('人均GDP PPP (美元)')
plt.ylabel('人类发展指数 (HDI)')
plt.grid(True, which="both", ls="--")
plt.tight_layout()
plt.show()
3.6 人口与面积的关系
使用scatterplot
绘制散点图,观察人口与面积之间的关系。以'Human Development Index (HDI)'
为着色依据,'Nominal GDP Per capita (in USD)'
调整气泡大小,对x轴和y轴均采用对数尺度,以更好地呈现数据分布特征。
plt.figure(figsize=(12, 8))
sns.scatterplot(data=df, x='Population (in millions)', y='AREA (in Sq km)',hue='Human Development Index (HDI)', size='Nominal GDP Per capita (in USD)',sizes=(20, 200), alpha=0.7)
plt.xscale('log')
plt.yscale('log')
plt.title('人口与面积关系图 (按HDI着色,气泡大小表示人均GDP)')
plt.xlabel('人口 (百万)')
plt.ylabel('面积 (平方公里)')
plt.grid(True, which="both", ls="--")
plt.tight_layout()
plt.show()
3.7 全球GDP分布直方图
利用seaborn
的histplot
函数绘制直方图,展示全球各国名义GDP的分布情况。设置bins=30
以确定直方图的柱数,并对x轴采用对数尺度,使数据分布更直观。
plt.figure(figsize=(12, 8))
sns.histplot(data=df, x='Nominal Gross Domestic Product (in USD)', bins=30, log_scale=True)
plt.title('全球GDP分布直方图 (对数尺度)')
plt.xlabel('名义GDP (美元)')
plt.ylabel('国家数量')
plt.grid(True, which="both", ls="--")
plt.tight_layout()
plt.show()
3.8 HDI全球分布箱线图
通过seaborn
的boxplot
函数绘制箱线图,呈现全球各国HDI的分布情况,直观展示数据的中位数、四分位数以及异常值等信息。
plt.figure(figsize=(12, 8))
sns.boxplot(data=df, y='Human Development Index (HDI)')
plt.title('全球HDI分布箱线图')
plt.ylabel('人类发展指数 (HDI)')
plt.grid(True, which="both", ls="--")
plt.tight_layout()
plt.show()
3.9 GDP前20国家
对数据集按'Nominal Gross Domestic Product (in USD)'
进行降序排序,选取前20个国家。使用barplot
绘制条形图展示这些国家的名义GDP总量情况。通过hue
参数根据国家名称区分颜色,采用'rocket'
调色板。同时,为优化x轴刻度显示,使用plt.gca().xaxis.set_major_formatter
方法,根据GDP数值大小自动调整刻度标签的显示格式(万亿或十亿)。
top_gdp = df.sort_values('Nominal Gross Domestic Product (in USD)', ascending=False).head(20)
plt.figure(figsize=(12, 8))
sns.barplot(data=top_gdp, x='Nominal Gross Domestic Product (in USD)', y='country_name', hue='country_name', palette='rocket')
plt.title('名义GDP总量前20国家')
plt.xlabel('名义GDP总量 (美元)')
plt.ylabel('国家')
plt.gca().xaxis.set_major_formatter(lambda x, pos: f'{x/1e12:.1f}万亿' if x >= 1e12 else (f'{x/1e9:.0f}十亿' if x > 0 else ''))
plt.grid(True, which="both", ls="--", axis='x')
plt.tight_layout()
plt.show()
3.10 PPP前20国家
按'GDP Per capita PPP (in USD)'
对数据集进行降序排序,选取前20个国家。利用barplot
绘制条形图展示这些国家的人均GDP(PPP)情况。通过hue
参数根据国家名称区分颜色,采用'rocket'
调色板,并添加网格线以增强图表可读性。
top_ppp = df.sort_values('GDP Per capita PPP (in USD)', ascending=False).head(20)
plt.figure(figsize=(12, 8))
sns.barplot(data=top_ppp, x='GDP Per capita PPP (in USD)', y='country_name', hue='country_name', palette='rocket')
plt.title('人均GDP(PPP)前20国家')
plt.xlabel('人均GDP(PPP) (美元)')
plt.ylabel('国家')
plt.grid(True, which="both", ls="--", axis='x')
plt.tight_layout()
plt.show()