I am new to Plotly and have received the following error when trying to create my first live streaming chart:
ValueError:
Invalid value of type ‘datetime.datetime’ received for the ‘x’ property of scatter
the chart opens in a new browser tab, but no data appears.
It is basically plotting randomly generated data received from another script via a socket connection in the code below. I was wondering if anyone else has had a similar same issue and how they resolved it. I have included the code and traceback below:
Here is the code:
#
# Streaming Plot with Plotly & ZeroMQ
#
import zmq
import plotly.plotly as ply
from plotly.graph_objs import *
import configparser
import datetime
# socket communication
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect(‘tcp://127.0.0.1:5555’)
socket.setsockopt_string(zmq.SUBSCRIBE, ‘’)
# plotly api tokens
c = configparser.ConfigParser()
c.read(‘python_config.cfg’)
api_tokens = c[‘plotly’][‘api_tokens’].split(’,’)
# plotly plot preparations
s = Stream(maxpoints=100, token=api_tokens[0])
t = Scatter(x=[], y=[], name=‘tick data’, mode=‘lines+markers’, stream=s)
d = Data([t])
l = Layout(title=‘Algo Tick Data’)
f = Figure(data=d, layout=l)
ply.plot(f, filename=‘algo_example’, auto_open=True)
s0 = ply.Stream(api_tokens[0])
s0.open()
while True:
msg = socket.recv_string()
t = datetime.datetime.now()
sym, bid, ask = msg.split()
s0.write({‘x’: t, ‘y’: float(bid)})
print(str(t) + ’ | ’ + msg)
And the traceback:
Traceback (most recent call last):
File “stream_plot.py”, line 40, in
s0.write({‘x’: t, ‘y’: float(bid)})
File “C:\Users\mgrif\Miniconda3\envs\finpy\lib\site-packages\plotly\plotly\plotly.py”, line 668, in write
dummy_obj = copy.deepcopy(Scatter(**stream_object))
File “C:\Users\mgrif\Miniconda3\envs\finpy\lib\site-packages\plotly\graph_objs_scatter.py”, line 2123, in init
self.x = x if x is not None else v
File “C:\Users\mgrif\Miniconda3\envs\finpy\lib\site-packages\plotly\basedatatypes.py”, line 2666, in setattr
super(BasePlotlyType, self).setattr(prop, value)
File “C:\Users\mgrif\Miniconda3\envs\finpy\lib\site-packages\plotly\graph_objs_scatter.py”, line 1301, in x
self[‘x’] = val
File “C:\Users\mgrif\Miniconda3\envs\finpy\lib\site-packages\plotly\basedatatypes.py”, line 2638, in setitem
self._set_prop(prop, value)
File “C:\Users\mgrif\Miniconda3\envs\finpy\lib\site-packages\plotly\basedatatypes.py”, line 2868, in _set_prop
val = validator.validate_coerce(val)
File “C:\Users\mgrif\Miniconda3\envs\finpy\lib\site-packages_plotly_utils\basevalidators.py”, line 313, in validate_coerce
self.raise_invalid_val(v)
File “C:\Users\mgrif\Miniconda3\envs\finpy\lib\site-packages_plotly_utils\basevalidators.py”, line 216, in raise_invalid_val
valid_clr_desc=self.description()))
ValueError:
Invalid value of type ‘datetime.datetime’ received for the ‘x’ property of scatter
Received value: datetime.datetime(2018, 7, 11, 16, 21, 35, 573446)
The 'x' property is an array that may be specified as a tuple,
list, numpy array, or pandas Series
and I receive the following deprecation warnings …
plotly.graph_objs.Stream is deprecated.
Please replace it with one of the following more specific types
- plotly.graph_objs.scatter.Stream
- plotly.graph_objs.area.Stream
plotly.graph_objs.Data is deprecated.
Please replace it with a list or tuple of instances of the following types
- plotly.graph_objs.Scatter
- plotly.graph_objs.Bar
- plotly.graph_objs.Area
- plotly.graph_objs.Histogram
- etc.
I’m not sure what the Value error above is complaining about. The value returned looks suspiciously like a tuple to me.
Many Thanks,