Mantej-Singh / Playing-with-Earthquakes-dataset

Using Plotly to create scatter plot on World Map
12 stars 3 forks source link

Repory Error #1

Open maihao14 opened 5 years ago

maihao14 commented 5 years ago

Hi Mantej,

I have seen you have done great work.

However, when I am trying your code at the last part. It reproted an error and cannot yeild the result intersurface you displayed. I am not sure what's going on? Could you have a look at the issue and help me fix that? Many thanks!

`ValueError: Invalid value of type 'builtins.str' received for the 'size' property of scattergeo.marker Received value: 'earthquake'

The 'size' property is a number and may be specified as:
  - An int or float in the interval [0, inf]
  - A tuple, list, or one-dimensional numpy array of the above

ValueError Traceback (most recent call last)

in 13 line = dict (color = colors[i],width = 0.5), 14 #sizemode = 'area', ---> 15 size = types[i] 16 #size = len(df_sub)/8324 17 ), ~\Anaconda3\lib\site-packages\plotly\graph_objs\__init__.py in __init__(self, arg, connectgaps, customdata, customdatasrc, fill, fillcolor, geo, hoverinfo, hoverinfosrc, hoverlabel, hovertemplate, hovertemplatesrc, hovertext, hovertextsrc, ids, idssrc, lat, latsrc, legendgroup, line, locationmode, locations, locationssrc, lon, lonsrc, marker, meta, metasrc, mode, name, opacity, selected, selectedpoints, showlegend, stream, text, textfont, textposition, textpositionsrc, textsrc, uid, uirevision, unselected, visible, **kwargs) 32791 self["lonsrc"] = lonsrc if lonsrc is not None else _v 32792 _v = arg.pop("marker", None) > 32793 self["marker"] = marker if marker is not None else _v 32794 _v = arg.pop("meta", None) 32795 self["meta"] = meta if meta is not None else _v ~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in __setitem__(self, prop, value) 3343 # ### Handle compound property ### 3344 if isinstance(validator, CompoundValidator): -> 3345 self._set_compound_prop(prop, value) 3346 3347 # ### Handle compound array property ### ~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in _set_compound_prop(self, prop, val) 3694 validator = self._validators.get(prop) 3695 # type: BasePlotlyType -> 3696 val = validator.validate_coerce(val, skip_invalid=self._skip_invalid) 3697 3698 # Save deep copies of current and new states ~\Anaconda3\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v, skip_invalid) 2431 2432 elif isinstance(v, dict): -> 2433 v = self.data_class(v, skip_invalid=skip_invalid) 2434 2435 elif isinstance(v, self.data_class): ~\Anaconda3\lib\site-packages\plotly\graph_objs\scattergeo\__init__.py in __init__(self, arg, autocolorscale, cauto, cmax, cmid, cmin, color, coloraxis, colorbar, colorscale, colorsrc, gradient, line, opacity, opacitysrc, reversescale, showscale, size, sizemin, sizemode, sizeref, sizesrc, symbol, symbolsrc, **kwargs) 2097 self["showscale"] = showscale if showscale is not None else _v 2098 _v = arg.pop("size", None) -> 2099 self["size"] = size if size is not None else _v 2100 _v = arg.pop("sizemin", None) 2101 self["sizemin"] = sizemin if sizemin is not None else _v ~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in __setitem__(self, prop, value) 3351 # ### Handle simple property ### 3352 else: -> 3353 self._set_prop(prop, value) 3354 3355 # Handle non-scalar case ~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in _set_prop(self, prop, val) 3634 return 3635 else: -> 3636 raise err 3637 3638 # val is None ~\Anaconda3\lib\site-packages\plotly\basedatatypes.py in _set_prop(self, prop, val) 3629 validator = self._validators.get(prop) 3630 try: -> 3631 val = validator.validate_coerce(val) 3632 except ValueError as err: 3633 if self._skip_invalid: ~\Anaconda3\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v) 780 # Check numeric 781 if not isinstance(v, numbers.Number): --> 782 self.raise_invalid_val(v) 783 784 # Check min/max ~\Anaconda3\lib\site-packages\_plotly_utils\basevalidators.py in raise_invalid_val(self, v, inds) 281 typ=type_str(v), 282 v=repr(v), --> 283 valid_clr_desc=self.description(), 284 ) 285 ) ValueError: Invalid value of type 'builtins.str' received for the 'size' property of scattergeo.marker Received value: 'earthquake' The 'size' property is a number and may be specified as: - An int or float in the interval [0, inf] - A tuple, list, or one-dimensional numpy array of the above `
Mantej-Singh commented 5 years ago

I'll take a look at it and reply to you with an answer asap.

Thanks

Mantej-Singh commented 5 years ago

Are you using the same dataset for plotting? Also, can you tell me a line number for your error? By looking at your error it seems you need to do typecasting, are you correctly passing the 'size' as an int?

maihao14 commented 5 years ago

Hi again, I download your *.csv and jupyter script. The error occurs at the visualization part: `cases = [] for i in range(len(types)): lim = types[i] df_sub = df.loc[df.type==types[i],:] cases.append(go.Scattergeo( lon = df_sub['longitude'], lat = df_sub['latitude'], text = df_sub['text'],

colorscale = scl,

    #autocolorscale = False,
    name = types[i] + ' : '+str(len((df_sub))),
    marker = dict(
        line = dict (color = colors[i],width = 0.5),
        #sizemode = 'area',
        size = types[i]
        #size = len(df_sub)/8324`
Mantej-Singh commented 5 years ago

thanks, I understood the problem and I am working on it. Reply to you soon.

Mantej-Singh commented 5 years ago

Hi @maihao14, I figured it out, the below error 'size' property is a number and may be specified as was caused because size = types[i] was returning a string.

So I commented it out from the code, I ran it the I got another error saying: AttributeError: 'Scattergeo' object has no attribute 'items' image

And the reason for this error was a dict which I used to iterate So I made changes to two lines in code, and it ran flawlessly.

Old:

marker = dict(
fig = dict( data=cases, layout=layout )

New:

marker = go.scattergeo.Marker(
fig = go.Figure( data=cases, layout=layout )