2017-10-15 238 views
1

我想繪製一個蠟燭棒圖在python中。這裏是我的代碼在Python中繪製蠟燭棒

from pandas_datareader import data as pdr 
import plotly.plotly as py 
import plotly.graph_objs as go 
import fix_yahoo_finance as yf 

yf.pdr_override() 
mcd = pdr.get_data_yahoo("MCD", start="2004-01-01", end="2005-07-31") 
mcd_candle = go.Candlestick(x=mcd.index,open=mcd.Open,high=mcd.High,low=mcd.Low,close=mcd.Close) 
data = [mcd_candle] 
py.iplot(data, filename='Candle Stick') 

這是錯誤我得到

PlotlyError: Because you didn't supply a 'file_id' in the call, we're assuming you're trying to snag a figure from a url. You supplied the url, '', we expected it to start with 'https://plot.ly'. 

任何想法,我怎麼能畫K線?

回答

3

問題必須是因爲您沒有提供usernameapi key,您將從https://plot.ly/settings/api鏈接中獲得。如果您想使用plotly online創建此圖表。首先創建一個帳戶,然後獲取usernameapi key並將其插入到下面的代碼中。

from pandas_datareader import data as pdr 
import plotly.plotly as py 
import plotly.graph_objs as go 
import fix_yahoo_finance as yf 
py.sign_in('<<username here>>', '<<api key here>>') 
yf.pdr_override() 
mcd = pdr.get_data_yahoo("MCD", start="2004-01-01", end="2005-07-31") 
mcd_candle = go.Candlestick(x=mcd.index,open=mcd.Open,high=mcd.High,low=mcd.Low,close=mcd.Close) 
data = [mcd_candle] 
py.iplot(data, filename='Candle Stick') 

有使用plotly offline,並不需要所有這些程序的另一種選擇,請看下面的實現代碼。

from pandas_datareader import data as pdr 
import plotly.offline as py_offline 
import plotly.graph_objs as go 
import fix_yahoo_finance as yf 
py_offline.init_notebook_mode() 

yf.pdr_override() 
mcd = pdr.get_data_yahoo("MCD", start="2004-01-01", end="2005-07-31") 
mcd_candle = go.Candlestick(x=mcd.index,open=mcd.Open,high=mcd.High,low=mcd.Low,close=mcd.Close) 
data = [mcd_candle] 
py_offline.iplot(data, filename='Candle Stick') 
#for Spyder plotting use the below line instead 
#py_offline.plot(data, filename='Candle Stick') 

請務必安裝pandas_datareaderfix_yahoo_finance使用pip庫,如果這些庫不存在!

+0

通過實施您的離線方法,我的代碼正在運行,但沒有看到圖形輸出。代碼運行時沒有任何錯誤。我正在使用Anaconda - Spyder。 Python 3.x –

+0

@DebdiptaMajumdar對不起,你需要在使用spyder時使用'py_offline.plot'而不是'py_offline.iplot',請參考[here](https://stackoverflow.com/questions/35315726/visualize- plotly-charts-in-spyder) –

+0

Naren Murali 非常感謝它的工作 你的天才:) –

相關問題