gpt4 book ai didi

python - 在 Python 中绘制蜡烛棒

转载 作者:行者123 更新时间:2023-11-28 22:23:55 24 4
gpt4 key购买 nike

我正在尝试在 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'.

知道如何绘制 Candle Stick Chart 吗?

最佳答案

问题一定是因为您没有提供您将从 https://plot.ly/settings 获得的 usernameapi key/api 链接。如果您想使用 plotly online 创建此图。首先创建一个帐户,然后获取用户名api 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')

如果这些库尚不存在,请确保使用 pip 安装库 pandas_datareaderfix_yahoo_finance!

关于python - 在 Python 中绘制蜡烛棒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46752321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com