Search code examples
pythonplotly

ValueError: Invalid element(s) received for the 'size' property of scattergeo.marker


I've encountered an issue with plotly. I can't seem to find the error in the code and it won't allow my graph to show.

import json

from plotly.graph_objs import Scattergeo, Layout
from plotly import offline

""" Get json file """
filename = 'all_week.json'
with open(filename, encoding='utf-8') as f:
    all_week_data = json.load(f)

""" Create readable file """
readable_file = 'readable_all_week.json'
with open(readable_file, 'w') as f:
    json.dump(all_week_data, f, indent=4)

""" Get data from dictionary """
eq_week_dicts = all_week_data['features']

""" Create list and store mags, lons, lats, labels """
mags, lons, lats, labels = [], [], [], []
for eq_week_dict in eq_week_dicts:
    mags.append(eq_week_dict['properties']['mag'])
    lons.append(eq_week_dict['geometry']['coordinates'][0])
    lats.append(eq_week_dict['geometry']['coordinates'][1])
    labels.append(eq_week_dict['properties']['title'])

""" Create Map """
data = [{
    'type': 'scattergeo',
    'lon': lons,
    'lat': lats,
    'text': labels,

    # Create marks on map
    'marker': {
        'size': [5*mag for mag in mags],
        'color': mags,
        'colorscale': 'Viridis',
        'reverscale': True,
        'colorbar': {'title': 'Magnitude'}
    }
}]

""" Output data """
title = all_week_data['metadata']['title']
my_layout = Layout(title=title)

fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='eqs.html')

Here is the error code I am receiving. I do not know how to fix it? Would a try clause work?

enter image description here


Solution

  • This code will hopefully solve the problem of the ValueError. Edit your code and then try it, it worked for me, and hopefully, it will work for your code as well.

    for eq_dict in all_eq_dicts:
        mag = eq_dict["properties"]["mag"]
        if mag > 0:
            mags.append(eq_dict["properties"]["mag"])
            lons.append(eq_dict["geometry"]["coordinates"][0])
            lats.append(eq_dict["geometry"]["coordinates"][1])
            hover_texts.append(eq_dict["properties"]["title"])