首页 > 解决方案 > 使用 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'

标签: python-3.xmatplotlibalpha-vantage

解决方案


我有同样的问题。

请注意这一行,其中包含列的名称:

 1. open   2. high    3. low  4. close  5. volume

因此,将这一行更改为列名:例如:

data['close'].plot()为。。改变data['4. close'].plot()


推荐阅读