使用Python绘制Alpha Vantage API图

时间:2018-09-12 03:06:58

标签: python-3.x matplotlib alphavantage

我是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 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。

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

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

因此,用列名称更改此行:例如:

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