Advertisement

Python金融数据挖掘 第6章 复习思考题2

阅读量:

从美国(USA)、瑞典(SWE)和瑞士(CHE)这三个国家获取Pandas_datareader库提供的世界银行数据库中的近二十年的GDP人均( capita)数据,并对其进行可视化分析

复制代码
  
    
 # 导入wb用于查询世界银行数据
    
 # http://ju.outofmemory.cn/entry/308589
    
 # https://pandas-datareader.readthedocs.io/en/latest/readers/world-bank.html?
    
 # highlight=pandas_datareader.wb
    
 # 导入包
    
 import pandas_datareader.wb as worldbank
    
 import matplotlib.pyplot as plt 
    
 import pandas as pd
    
 import seaborn as sns
    
  
    
 # 图表内嵌中文字体问题
    
 plt.rcParams['font.sans-serif'] = ['SimHei']
    
 plt.rcParams['axes.unicode_minus'] = False
    
 sns.set_style({'font.sans-serif':['simhei','Arial']})
    
  
    
 # 起止年,近20年(2021为NAN,故采用2000-2020年数据)
    
 start_year = 2000
    
 end_year = 2020
    
  
    
 # 下载数据三国美国(USA)、瑞典(SWE)、瑞士(CHE)的"NY.GDP.PCAP.KD"数据
    
 date = worldbank.download(indicator = "NY.GDP.PCAP.KD",country = ["USA","SWE","CHE"],start = start_year,end = end_year)
    
 print('"NY.GDP.PCAP.KD数据:',date)
    
  
    
 # 合并数据
    
 dates = date.unstack(level=0)
    
  
    
 # 画图
    
 plt.plot(dates.iloc[:,0],'g-',label="SWE") #Sweden 瑞典
    
 plt.plot(dates.iloc[:,1],'b-*',label="CHE") #Switzerland 瑞士
    
 plt.plot(dates.iloc[:,2],'r--',label="USA") #United States 美国
    
  
    
 plt.title('美国、瑞典、瑞士三国人均GDP对比',fontsize=20)
    
 plt.legend()
    
 plt.savefig('美国、瑞典、瑞士三国GDP.png',dpi=300)
    
 plt.show()

从图表中可以看出, 我们能够观察到美国、瑞典以及瑞士这三个国家的人均GDP呈现出持续增长的趋势, 其中人均GDP的数据显示: 瑞士高于瑞典, 并高于美国。

全部评论 (0)

还没有任何评论哟~