绘制烛台更新并在循环中动态显示
我想从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码对我来说很好。顺便说一句,我嘲笑您的系列数据 - 当提出这样的问题提供一些示例数据时,
这确实是每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
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
尝试这些想法中的任何一个以查看它们是否有效,我无法尝试它们,因为我没有您的data frame:
time.sleep(3)
fig.show()
,而true
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:
time.sleep(3)
beforefig.show()
while true
candle_df = candle_handler.get_dataframe()
outside the loop