Plotly流媒体API(Python)不支持烛台图



问题

我正在尝试使用python绘图来制作显示实时数据的烛台图。因此,我正在使用Plotly的流API。例如,我只是调整了此链接中提供的示例。

工作代码

import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
import numpy as np  
import datetime
import time
stream_ids = tls.get_credentials_file()['stream_ids']
# Get stream id from stream id list
stream_id = stream_ids[0]
# Make instance of stream id object
stream = Stream(
    token=stream_id,  
    maxpoints=80      
)
# Initialize trace of streaming plot by embedding the unique stream_id
trace1 = Candlestick(
    open=[],
    high=[],
    low=[],
    close=[],
    x=[],
    stream=stream         
)
data = Data([trace1])
# Add title to layout object
layout = Layout(title='CANDLE STICK CHART')
# Make a figure object
fig = Figure(data=data, layout=layout)
# Send fig to Plotly, initialize streaming plot, open new tab
unique_url = py.plot(fig, filename='s7_first-stream')
# Make instance of the Stream link object,
s = py.Stream(stream_id)
# Open the stream
s.open()
i = 0    # a counter
k = 5    # some shape parameter
N = 200  # number of points to be plotted
# Delay start of stream by 5 sec (time to switch tabs)
time.sleep(5)
while i<N:
    i += 1   # add to counter
    # Current time on x-axis, dummy data for the candlestick chart
    x = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
    low = np.cos(k*i/50.)*np.cos(i/50.)+np.random.randn(1)
    close = low+1
    open = low +2
    high = low+3
    # Write to Plotly stream
    s.write(dict(x=x,
                 low=low,
                 close=close,
                 open=open,
                 high=high))
    # Write numbers to stream to append current data on plot,
    # Write lists to overwrite existing data on plot 
    time.sleep(0.08)  
# Close the stream when done plotting
s.close()

输出

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/plotly/plotly.py", line 632, in write
    tools.validate(stream_object, stream_object['type'])
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/tools.py", line 1323, in validate
    cls(obj)  # this will raise on invalid keys/items
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/graph_objs/graph_objs.py", line 377, in __init__
    self.__setitem__(key, val, _raise=_raise)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/graph_objs/graph_objs.py", line 428, in __setitem__
    raise exceptions.PlotlyDictKeyError(self, path)
plotly.exceptions.PlotlyDictKeyError: 'open' is not allowed in 'scatter'
Path To Error: ['open']
Valid attributes for 'scatter' at path [] under parents []:
    ['cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx',
    'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'hoverinfo',
    'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertext', 'hovertextsrc',
    'ids', 'idssrc', 'legendgroup', 'line', 'marker', 'mode', 'name',
    'opacity', 'r', 'rsrc', 'showlegend', 'stream', 't', 'text',
    'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'tsrc',
    'type', 'uid', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xsrc', 'y',
    'y0', 'yaxis', 'ycalendar', 'ysrc']
Run `<scatter-object>.help('attribute')` on any of the above.
'<scatter-object>' is the object at []
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/Louis/PycharmProjects/crypto_dashboard/Data_gathering/candlestick_stream_test.py", line 79, in <module>
    high=high))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/plotly/plotly.py", line 640, in write
    "invalid:nn{1}".format(stream_object['type'], err)
plotly.exceptions.PlotlyError: Part of the data object with type, 'scatter', is invalid. This will default to 'scatter' if you do not supply a 'type'. If you do not want to validate your data objects when streaming, you can set 'validate=False' in the call to 'your_stream.write()'. Here's why the object is invalid:
'open' is not allowed in 'scatter'
Path To Error: ['open']
Valid attributes for 'scatter' at path [] under parents []:
    ['cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx',
    'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'hoverinfo',
    'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertext', 'hovertextsrc',
    'ids', 'idssrc', 'legendgroup', 'line', 'marker', 'mode', 'name',
    'opacity', 'r', 'rsrc', 'showlegend', 'stream', 't', 'text',
    'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'tsrc',
    'type', 'uid', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xsrc', 'y',
    'y0', 'yaxis', 'ycalendar', 'ysrc']
Run `<scatter-object>.help('attribute')` on any of the above.
'<scatter-object>' is the object at []

看来我的跟踪被解释为"散布"类型的数据对象,而不是"烛台"。这可以解释为什么不允许"打开"属性。

流媒体API是由烛台对象支持的还是我缺少某些内容?

谢谢!

在您的while循环中,您应该将键type='candlestick'添加到传递给write的dict:

s.write(dict(type='candlestick',
    x=x,
    low=low,
    close=close,
    open=open,
    high=high))

它适用于接受stream属性的所有plotly.graph_objs跟踪。

最新更新