绘制烛台更新并在循环中动态显示

发布于 2025-01-31 16:27:51 字数 852 浏览 3 评论 0原文

我想从plotly中动态更新candlestick图表:

import time
import plotly.graph_objects as go

while True:
  candle_df = candle_handler.get_dataframe()
  candlestick = go.Candlestick(x=candle_df['Time'], open=candle_df['Open'], high=candle_df['High'], low=candle_df['Low'], close=candle_df['Close'])  
  fig = go.Figure(data=[candlestick])
  fig.show()
  time.sleep(3)

其中candle_handler.get_dataframe()从API中提取数据并在candle_df pandas dataframe。

但是,在循环的每次迭代中,图表仅显示非常简短(比3秒钟少得多)。

我找到了一个适用于散点图的片段:

import time
import plotly.graph_objects as go

data = [1,3,2,4,3,3,2,3]

fig = go.FigureWidget()
fig.add_scatter()

for i in range(len(data)):
  time.sleep(3)
  fig.data[0].y = data[:i]
  fig.show()

我想为Candlestick图表做类似的事情。

I would like to dynamically update a Candlestick chart from plotly:

import time
import plotly.graph_objects as go

while True:
  candle_df = candle_handler.get_dataframe()
  candlestick = go.Candlestick(x=candle_df['Time'], open=candle_df['Open'], high=candle_df['High'], low=candle_df['Low'], close=candle_df['Close'])  
  fig = go.Figure(data=[candlestick])
  fig.show()
  time.sleep(3)

where candle_handler.get_dataframe() pull the data from an API and updates the data in the candle_df pandas dataframe.

However, the chart is shown only very briefly at every iteration of the loop (much less than for 3 seconds).

I have found a snippet that works for a scatter plot:

import time
import plotly.graph_objects as go

data = [1,3,2,4,3,3,2,3]

fig = go.FigureWidget()
fig.add_scatter()

for i in range(len(data)):
  time.sleep(3)
  fig.data[0].y = data[:i]
  fig.show()

and I would like to do something similar for the Candlestick chart.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2025-02-07 16:27:51

您的代码对我来说很好。顺便说一句,我嘲笑您的系列数据 - 当提出这样的问题提供一些示例数据时,

    import time
    
    import numpy as np
    import pandas as pd
    import plotly.graph_objects as go
    import datetime
    
    class Handler:
      def get_dataframe(self):
        start = datetime.datetime(2011, 1, 1)
        tim_col = pd.date_range(start, periods=500, freq="D")
        tim_col.name = "Time"
        prices_df = pd.DataFrame(data=np.random.rand(500,4), columns=['Open','High','Low','Close'], index=tim_col).reset_index()
        prices_df['Open'] = prices_df['Open']+ prices_df['Low']
        prices_df['Close'] = prices_df['Close']+ prices_df['Low']
        prices_df['High'] = prices_df['High']+ prices_df[['Open','Close']].max(axis=1)
        return prices_df
    
    candle_handler = Handler()
    
    while True:
      candle_df = candle_handler.get_dataframe()
      candlestick = go.Candlestick(x=candle_df['Time'], open=candle_df['Open'], high=candle_df['High'], low=candle_df['Low'], close=candle_df['Close'])
      fig = go.Figure(data=[candlestick])
      fig.show()
      time.sleep(3)

这确实是每3秒钟的绘制数据。确切的行为将取决于您的图形渲染器。如果您在终端中运行它,它可能会在浏览器中弹出一个选项卡,查看每个循环中的Localhost,可能不是您想要的。如果您在jupyter笔记本中运行它,它将动态更改一个数字,这可能更接近您想要的东西。

如果要动态更新浏览器中的数字,则可以考虑查看 dash

Your code worked perfectly fine for me. As an aside, I mocked up some data for your series - it's good practice when asking a question like this to provide some sample data

    import time
    
    import numpy as np
    import pandas as pd
    import plotly.graph_objects as go
    import datetime
    
    class Handler:
      def get_dataframe(self):
        start = datetime.datetime(2011, 1, 1)
        tim_col = pd.date_range(start, periods=500, freq="D")
        tim_col.name = "Time"
        prices_df = pd.DataFrame(data=np.random.rand(500,4), columns=['Open','High','Low','Close'], index=tim_col).reset_index()
        prices_df['Open'] = prices_df['Open']+ prices_df['Low']
        prices_df['Close'] = prices_df['Close']+ prices_df['Low']
        prices_df['High'] = prices_df['High']+ prices_df[['Open','Close']].max(axis=1)
        return prices_df
    
    candle_handler = Handler()
    
    while True:
      candle_df = candle_handler.get_dataframe()
      candlestick = go.Candlestick(x=candle_df['Time'], open=candle_df['Open'], high=candle_df['High'], low=candle_df['Low'], close=candle_df['Close'])
      fig = go.Figure(data=[candlestick])
      fig.show()
      time.sleep(3)

This does indeed ahow a graph every 3 seconds. Exact behaviour is going to depend on your graph renderer. If you run it in the terminal, it will probably pop up a tab in your browser looking at localhost on every loop - probably not what you want. If you run it in a Jupyter Notebook, it will change a figure dynamically, which might be closer to what you want.

If you want to dynamically update a figure in the browser, you might consider looking at dash

花之痕靓丽 2025-02-07 16:27:51

尝试这些想法中的任何一个以查看它们是否有效,我无法尝试它们,因为我没有您的data frame:

  • put time.sleep(3) fig.show()
  • 使用循环而不是,而true
  • 也许put candle_df = candle_handler.get_dataframe()在循环外

Try any of these ideas to see if they work, I cannot try them out as I do not have you dataframe:

  • Put time.sleep(3) before fig.show()
  • Use a loop instead of a while true
  • Maybe put candle_df = candle_handler.get_dataframe() outside the loop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文