如何使用Plotly(Python)制作蜡烛图动画

3
尊敬的,
我可以使用中的Candlestick制作图表。 但是,在循环内部,如何使这些图形不是一个接一个地创建,而是全部在一个图形中?创建动画效果。 请参见示例,其中我连续绘制具有5天前瞻窗口的蜡烛,遍历整个DataFrame。
import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

#df
#i         Date   AAPL.Open   AAPL.High    AAPL.Low  AAPL.Close  AAPL.Volume  \
#0    2015-02-17  127.489998  128.880005  126.919998  127.830002     63152400   
#1    2015-02-18  127.629997  128.779999  127.449997  128.720001     44891700  
#....

for index, row in df.iterrows():
    
    FiveDaysBlock = df.iloc[index:(index+5),]

    fig = go.Figure(data=[go.Candlestick(
    x=FiveDaysBlock['Date'],
    open=FiveDaysBlock['AAPL.Open'], high=FiveDaysBlock['AAPL.High'],
    low=FiveDaysBlock['AAPL.Low'], close=FiveDaysBlock['AAPL.Close']
    )])
    fig.update_layout(xaxis_rangeslider_visible=False)
    fig.update_layout(autosize=False,width=700, height=250, margin=dict( l=1,r=1,b=20,  t=20, pad=2 ) )
    fig.update_xaxes(rangebreaks=[dict(bounds=["sat", "mon"])])
    fig.show()

See how the figure is generated under the other

谢谢您,


在动画的每个步骤中,您想要下一个 FiveDaysBlock 替换当前的块吗? - Derek O
@DerekO 是的,我想要。 - Lucas Pereira
2个回答

1

0

很遗憾,根据这个未解决的问题,似乎使用go.Candlestick()对象时x轴和y轴自动缩放被禁用了,因此随着动画的进行,轴无法正确缩放。

import plotly.graph_objects as go
import datetime as dt
import pandas as pd
from ipywidgets import widgets
from IPython.display import display

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')

#df
#i         Date   AAPL.Open   AAPL.High    AAPL.Low  AAPL.Close  AAPL.Volume  \
#0    2015-02-17  127.489998  128.880005  126.919998  127.830002     63152400   
#1    2015-02-18  127.629997  128.779999  127.449997  128.720001     44891700  
#....

# it might be useful to convert the Dates to datetime objects
df['Date'] = pd.to_datetime(df['Date'])

candlestick_data = []
for index, row in df.iterrows():
    FiveDaysBlock = df.iloc[index:(index+5),]
    candlestick_data.append(go.Candlestick(x=FiveDaysBlock['Date'],
        open=FiveDaysBlock['AAPL.Open'], high=FiveDaysBlock['AAPL.High'],
        low=FiveDaysBlock['AAPL.Low'], close=FiveDaysBlock['AAPL.Close'])
    )

def update_fig(i):
    fig = go.Figure(data=candlestick_data[i])

    fig.update_layout(xaxis_rangeslider_visible=False)
    fig.update_layout(transition_duration=2000)
    fig.update_layout(autosize=True,width=700, height=250, margin=dict(l=1,r=1,b=20,t=20,pad=2))

# this doesn't quite work but it's close
interact(update_fig, i=100)
fig.show()

1
亲爱的@DerekO,感谢您对回复的关注。 不幸的是,点击[自动缩放]按钮不是可行的过程。 如果无法制作动画,是否有“向前”和“向后”按钮来滚动5天窗口列表?我最好的祝愿, - Lucas Pereira
我有一种感觉,使用go.Candlestick()对象会遇到问题,因为轴无法在动画或帧进度中调整大小。因此,您将需要手动从每个帧绘制框。如果我有时间,我肯定可以尝试一下。最好的祝福,Derek - Derek O
我明白了,谢谢。是否可以使用交互式滑块来滚动创建的窗口?而不使用动画。例如:https://blog.dominodatalab.com/interactive-dashboards-in-jupyter/ - Lucas Pereira
说实话,我没有在Jupyter中创建交互式仪表板的经验,但“Interact”功能看起来很有前途。您可以放弃Plotly动画,改用交互式滑块来更改K线图的输出。如果您需要指南,我会更新我的答案以帮助您入门。 - Derek O
我目前的编写方式是,交互按钮不会更新显示的图形。这可能与 plotly 的后端有关:https://github.com/ipython/ipython/issues/7520 - Derek O
很遗憾,它没有工作...返回:AttributeError:模块'plotly.graph_objects'没有属性'show'。 - Lucas Pereira

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接