Have trouble while creating multiple traces with a loop

Hello. I’m a beginner in Plotly, and I’m trying to use this for my school project.

In my code, I’m making 20 traces with three arrays(SensorX for x, SensorY for y, Detection for color) with 20 components each by indexing the array, but it doesn’t work.

Here is my code

import plotly
import plotly.graph_objs as go
import numpy as np
import matplotlib.pyplot as plt

R = 1
M_Sin = np.sin(np.arange(0,2*np.pi,2*np.pi/20))
M_Cos = np.cos(np.arange(0,2*np.pi,2*np.pi/20)) 
SensorX = 1*M_Cos
SensorY = 1*M_Sin

Source = [-0.202,0.621]
Distance = np.sqrt(np.square(SensorX-Source[0]) + np.square(SensorY-Source[1]))
Origin_Detect = np.power(0.90, Distance/0.1)
Add_Noise = np.random.normal(loc=0,scale=0.01,size=20)
Detection = (Origin_Detect + Add_Noise) * 0.95

plotly.offline.init_notebook_mode(connected=True)

for i in range(20):
    vars()['trace'+str(i)] = go.Scatter(
        x = SensorX[i],
        y = SensorY[i],
        mode = "markers",
        name = "detector%d"%(i)
        marker = dict(size = 10,
                      color = Detection[i])
    )

layout = dict(title = "scheme3",
             xaxis = dict(title = 'x', ticklen = 20, zeroline = False),
             yaxis = dict(title = 't', ticklen = 20, zeroline = False)
             )
for i in range(20):
    fig = go.Figure(data = vars()['trace'+str(i)] , layout = layout)
 
plotly.offline.iplot(fig)

Here is the error message

ValueError Traceback (most recent call last)
in
79 mode = “markers”,
80 name = “detector%d”%(i),
—> 81 marker = dict(size = 10, color = Detection[i])
82 )
83

~/anaconda3/lib/python3.6/site-packages/plotly/graph_objs/init.py in init(self, arg, cliponaxis, connectgaps, customdata, customdatasrc, dx, dy, error_x, error_y, fill, fillcolor, groupnorm, hoverinfo, hoverinfosrc, hoverlabel, hoveron, hovertemplate, hovertemplatesrc, hovertext, hovertextsrc, ids, idssrc, legendgroup, line, marker, meta, metasrc, mode, name, opacity, orientation, r, rsrc, selected, selectedpoints, showlegend, stackgaps, stackgroup, stream, t, text, textfont, textposition, textpositionsrc, textsrc, tsrc, uid, uirevision, unselected, visible, x, x0, xaxis, xcalendar, xsrc, y, y0, yaxis, ycalendar, ysrc, **kwargs)
39647 self[‘visible’] = visible if visible is not None else _v
39648 _v = arg.pop(‘x’, None)

39649 self[‘x’] = x if x is not None else _v
39650 _v = arg.pop(‘x0’, None)
39651 self[‘x0’] = x0 if x0 is not None else _v

~/anaconda3/lib/python3.6/site-packages/plotly/basedatatypes.py in setitem(self, prop, value)
3315 # ### Handle simple property ###
3316 else:
→ 3317 self._set_prop(prop, value)
3318
3319 # Handle non-scalar case

~/anaconda3/lib/python3.6/site-packages/plotly/basedatatypes.py in _set_prop(self, prop, val)
3560 return
3561 else:
→ 3562 raise err
3563
3564 # val is None

~/anaconda3/lib/python3.6/site-packages/plotly/basedatatypes.py in _set_prop(self, prop, val)
3555 validator = self._validators.get(prop)
3556 try:
→ 3557 val = validator.validate_coerce(val)
3558 except ValueError as err:
3559 if self._skip_invalid:

~/anaconda3/lib/python3.6/site-packages/_plotly_utils/basevalidators.py in validate_coerce(self, v)
374 v = to_scalar_or_list(v)
375 else:
→ 376 self.raise_invalid_val(v)
377 return v
378

~/anaconda3/lib/python3.6/site-packages/_plotly_utils/basevalidators.py in raise_invalid_val(self, v, inds)
275 typ=type_str(v),
276 v=repr(v),
→ 277 valid_clr_desc=self.description()))
278
279 def raise_invalid_elements(self, invalid_els):

ValueError:
Invalid value of type ‘numpy.float64’ received for the ‘x’ property of scatter
Received value: 1.0

The 'x' property is an array that may be specified as a tuple,
list, numpy array, or pandas Series

Hi @danielhankim,

You are getting this error because the x and y properties of a go.Scatter trace need to be set as lists or arrays, but it looks like you are setting them to scalar numbers. If you really intend for each trace to contain a single point, then you could wrap the x and y property values in a list (e.g. SensorX[i]).

-Jon