Jupiter notebook 做五个国家近年来人均GDP增长最快的折线图
时间: 2023-12-15 14:07:17 浏览: 31
首先,我们需要获取五个国家近年来的人均GDP数据。这里我们可以使用世界银行提供的数据集,可以通过pandas库直接获取。
```python
import pandas as pd
# 获取世界银行提供的人均GDP数据集
url = 'http://api.worldbank.org/v2/en/indicator/NY.GDP.PCAP.CD?downloadformat=csv'
data = pd.read_csv(url, skiprows=4)
# 选择近年来的数据,这里我们选择2015年至2019年的数据
data = data[['Country Name', '2015', '2016', '2017', '2018', '2019']]
data = data.dropna() # 删除缺失值
# 计算年均增长率
data['avg_growth'] = data[['2015', '2016', '2017', '2018', '2019']].apply(lambda x: (x[4]/x[0])**(1/4)-1, axis=1)
# 按年均增长率排名,选择前五个国家
data = data.sort_values(by='avg_growth', ascending=False)[:5]
```
接下来,我们可以使用matplotlib库画出折线图。
```python
import matplotlib.pyplot as plt
# 设置图形大小
plt.figure(figsize=(10, 6))
# 画出每个国家的折线图
for i, row in data.iterrows():
country = row['Country Name']
values = row[['2015', '2016', '2017', '2018', '2019']].values
plt.plot(range(2015, 2020), values, label=country)
# 设置图形标题和标签
plt.title('Top 5 Countries with the Fastest GDP Growth')
plt.xlabel('Year')
plt.ylabel('GDP per capita (current US$)')
# 添加图例
plt.legend()
# 显示图形
plt.show()
```
运行上述代码,即可得到五个国家近年来人均GDP增长最快的折线图。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)