2011-10-17 125 views
5

我安裝了Enthought的EPD(64位Windows 7)。Ipython/pylab/matplotlib繪圖錯誤

我試圖用雅虎的API繪製歷史股票報價數據。我試圖使用的所有代碼都在以下博客文章中: http://www.traineetrader.com/importing-stock-data-from-yahoo-using-python/

ystockquote.py文件正常工作。

但第二個腳本來繪製谷歌的歷史股票報價不適用於我。這是代碼(從網站):

import ystockquote 

# Get Quotes 01/01/2006 - 01/01/2009 
GOOG = ystockquote.get_historical_prices('GOOG', '20060101', '20090101') 

# Create empty lists, quick and dirty 
GOOGOpen = [ ] 
GOOGClose = [ ] 
GOOGDate = [ ] 
GOOGHigh = [ ] 
GOOGLow = [ ] 
GOOGAdj = [ ] 
GOOGVolume = [ ] 

# Populate lists from downloaded data 
for i in range(1, 755): 
    GOOGDate.append(GOOG[i][0]) 
    GOOGOpen.append(GOOG[i][1]) 
    GOOGHigh.append(GOOG[i][2]) 
    GOOGLow.append(GOOG[i][3]) 
    GOOGClose.append(GOOG[i][4]) 
    GOOGVolume.append(GOOG[i][5]) 
    GOOGAdj.append(GOOG[i][6]) 

plot(GOOGAdj) 
title("Google Adjusted Close") 
ylabel(r"GOOG Closing Price ($USD)", fontsize = 12) 
xlabel(r"Date", fontsize = 12) 
grid(True) 

我得到以下錯誤:

NameError: name 'plot' is not defined 

什麼,我做錯了什麼建議?或者如何讓這個運行?如果我在代碼的頂部包含「from pylab import *」,我不會收到錯誤信息,但沒有任何反應。

+0

我要指出,我正在從PyLab解釋這個腳本。 –

+2

N.B.如果你啓動了'ipython --pylab',它會自動加載pylab的東西,並且還使用一些技巧,所以控制檯在打開窗口時不會阻塞。 –

回答

7

除了添加from pylab import *之外,您需要在腳本的最後一行(即grid(True)之後)後添加show()以實際顯示繪圖。

這是我加入show()後得到:

Google Adjusted Close

+0

當然,這個圖與鏈接到問題中的[博客文章](http://www.traineetrader.com/importing-stock-data-from-yahoo-using-python/)中的圖相匹配。 –

+0

啊!這也適用於我。非常感謝。 –