【问题标题】:How to make an animation with Candlestick from plotly (Python)如何使用 Candlestick 从 plotly (Python) 制作动画
【发布时间】:2023-03-08 18:21:02
【问题描述】:

尊敬的,

我可以用 plotly 的烛台制作图。 但是,在一个循环中,如何使这些图形不是一个在另一个之下,而是全部在一个图形中?创建动画效果。 请参阅我在 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 替换当前动画的每一步吗?
  • @DerekO 是的,我想要。

标签: python pandas plotly


【解决方案1】:

这几天我也在尝试做同样的事情,最后我做到了:

这里的代码:

https://github.com/matiaslee/candlestick_animation_with_plotly/blob/main/candlestick_animation_with_plotly.ipynb

最好的!

【讨论】:

    【解决方案2】:

    不幸的是,根据以下 [answer][1],似乎使用 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()
    

    【讨论】:

    • 亲爱的@DerekO,感谢您对回复的关注。不幸的是,必须单击 [Autoscale] 按钮不是一个可行的过程。如果无法制作动画,是否会有“前进”和“后退”按钮来滚动浏览 5 天窗口列表?我最好的,
    • 我有一种感觉,使用 go.Candlestick() 对象会遇到问题,因为轴无法随着动画或帧进度调整大小。因此,您将手动从每个帧中绘制框。如果我有时间,我当然可以试一试。最好的,德里克
    • 我明白了,谢谢。是否可以使用交互式滑块滚动创建的窗口?不使用动画。示例:blog.dominodatalab.com/interactive-dashboards-in-jupyter
    • 说实话,我没有在 Jupyter 中创建交互式仪表板的经验,但“交互”功能看起来很有前途。您可以废弃 Plotly 动画,而是使用交互式滑块来更改烛台图的输出。如果您需要指南,我会更新我的答案以帮助您入门。
    • 按照我现在写的方式,交互按钮不会更新显示的图形。这可能与plotly的后端有关:github.com/ipython/ipython/issues/7520
    猜你喜欢
    • 2020-07-25
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2011-02-02
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多