问题遇到的现象和发生背景
对六家公司的股价走势进行比较,并画出曲线图
问题相关代码,请勿粘贴截图
from pandas_datareader import data
import pandas as pd
import matplotlib.pyplot as plt
font = {'family':'SimHei','weight':'bold','size':'12'}
plt.rc('font',**font) #设置字体的更多属性
plt.rc('axes',unicode_minus = False) #坐标轴的负号显示
#一:获取数据
#1.定义要获取的股票数据的来源和时间区间
gafataDict = {'谷歌':'GOOD','亚马逊':'YMZN','Facebook':'FB',
'苹果':'AAPL','阿里巴巴':'BABA','腾讯':'0700.hk',}
start_data = '2021-01-01'
end_data = '2022-01-22'
#2.分别获取阿里巴巴等六家公司的股票数据
#datareader用yahoo获取历史数据,但是21年11月1日大陆停止运行
ALData = data.get_data_stooq(gafataDict['阿里巴巴'],start_data,end_data)
FBData = data.get_data_stooq(gafataDict['Facebook'],start_data,end_data)
# TXData = data.get_data_stooq(gafataDict['腾讯'],start_data,end_data)
GgData = data.get_data_stooq(gafataDict['谷歌'],start_data,end_data)
YmaxData = data.get_data_stooq(gafataDict['亚马逊'],start_data,end_data)
AppleData = data.get_data_stooq(gafataDict['苹果'],start_data,end_data)
ax1 = GgData.plot(y='Close',label='谷歌')
#在同一画布上绘图
YmaxData.plot(ax=ax1,y='Close',label='亚马逊')
# TXData.plot(ax = ax1,y = 'Close_dollar',label = '腾讯')
FBData.plot(ax=ax1,y='Close',label='Facebook')
AppleData.plot(ax=ax1,y='Close',label='苹果')
ALData.plot(ax=ax1,y='Close',label='阿里巴巴')
plt.xlabel('时间')
plt.ylabel('股价(美元)')
plt.show()
运行结果及报错内容
Traceback (most recent call last):
File "C:\Program Files\Python35\lib\site-packages\pandas\core\indexes\base.py", line 2897, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Close'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/数据分析/综合案例/股票数据分析.py", line 71, in <module>
YmaxData.plot(ax=ax1,y='Close',label='亚马逊')
File "C:\Program Files\Python35\lib\site-packages\pandas\plotting\_core.py", line 780, in __call__
data = data[y].copy()
File "C:\Program Files\Python35\lib\site-packages\pandas\core\frame.py", line 2995, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Program Files\Python35\lib\site-packages\pandas\core\indexes\base.py", line 2899, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Close'