python-3.x - 使用 Python 绘制 Alpha Vantage API
问题描述
我是 Python 新手,更具体地说是 Alpha Vantage。我的问题是我的代码正在收集股票的数据,但它没有绘制图表。我用 3.7 python 在我的 cmd 上运行了这段代码,并且已经更新了我所有的包。我听说人们在使用 matplotlib 和 3.7 版本的 python 时遇到问题,但我真的很想弄清楚这个 API。这是我下面的代码:
from alpha_vantage.timeseries
import TimeSeries
import matplotlib.pyplot as plt
import sys
def stockchart(symbol):
ts = TimeSeries(key='1ORS1XLM1YK1GK9Y', output_format='pandas')
data, meta_data = ts.get_intraday(symbol=symbol, interval='1min', outputsize='full')
print (data)
data['close'].plot()
plt.title('Stock chart')
plt.show()
symbol=input("Enter symbol name:") stockchart(symbol)
在为符号名称输入 MSFT 后,我在我的 cmd 上得到了这个响应......这意味着我正在从 API 中提取,但 data['close'] 函数无法与 PANDAS 一起正常工作
1. open 2. high 3. low 4. close 5. volume
date
2018-09-05 09:30:00 111.1900 111.4000 111.1200 111.3500 673119.0
File "C:\Users\davis\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\internals.py", line 4115, in get
loc = self.items.get_loc(item)
File "C:\Users\davis\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'close'
解决方案
我有同样的问题。
请注意这一行,其中包含列的名称:
1. open 2. high 3. low 4. close 5. volume
因此,将这一行更改为列名:例如:
data['close'].plot()
为。。改变data['4. close'].plot()
推荐阅读
- c++ - 如何在没有小数位限制的c ++中进行浮点数
- angular - 角度保存/恢复组件状态
- android-volley - 使用 Volley 与 BigCommerce 的 Orders API 端点交互时,如何解决“未知主机异常”?
- sql - 多行到列值按列在 SQL Server 中分组
- python-3.x - Python中的PyVisa.errors.VisaIOError异常处理
- prebid.js - 我可以在 prebid 中更改价格粒度吗
- python - 如何从对象访问值
- powershell - Powershell 列出所有名为“Create”的方法
- r - 与转移论点的递归关系
- javascript - 使用 {{data}} 编辑数据 Firestore