因此,我想要创建一个程序,在一个图表上显示过去30天的地震,我一直在使用这些GeoJSON数据:
https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson
这是我的节目。我不太确定该怎么办。当我被授予UnicodeDecodeError包括encoding="utf-8“时,有人告诉我。
任何帮助都会很好
Main.py
import json
from plotly.graph_objs import Scattergeo, Layout
from plotly import offline
#Read the structure of the file
readable_file = "Part 2\Data\JSON Files\Readable.json"
original_file = "Part 2\Data\JSON Files\EQ1.json"
with open(original_file, encoding="utf-8") as f:
all_eq_data = json.load(f)
# To write in the readable file:
with open(readable_file, "w") as f:
json.dump(all_eq_data, f, indent= 4)
print(len(all_eq_data))
#Info Grabbing
all_eq_data = all_eq_data["features"]
mags, longitude, latitude= [],[],[]
for eq_dict in all_eq_data:
mags.append(eq_dict["properties"]["mag"])
longitude.append(eq_dict["geometry"]["coordinates"][0])
latitude.append(eq_dict["geometry"]["coordinates"][1])
print(mags[:10])
print(longitude[:5])
print(latitude[:5])
#Plotting the earthquakes
data = [{
"type": "scattergeo",
"lon": longitude,
"lat": latitude,
"marker": {
"size": [5*mag for mag in mags],
"color": mags,
"colorscale": "Viridis",
"reversescale": True,
"colorbar": {"title": "Magnitude"},
},
}]
custom_layout = Layout(title= "Global Earthquakes")
fig = {"data": data, "layout": custom_layout}
offline.plot(fig, filename="global_earhtquakes.html")但是当我运行这个程序时,我遇到了一个巨大的错误:
Traceback (most recent call last):
File "c:\Users\amanm\Desktop\Python\Python Crash Course\Part 2\Data\Downloading Data 2.py", line 51, in <module>
offline.plot(fig, filename="global_earhtquakes.html")
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\offline\offline.py", line 573, in plot
figure = tools.return_figure_from_figure_or_data(figure_or_data, validate)
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\tools.py", line 553, in return_figure_from_figure_or_data
figure = Figure(**figure).to_dict()
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\graph_objs\_figure.py", line 596, in __init__
super(Figure, self).__init__(data, layout, frames, skip_invalid, **kwargs)
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\basedatatypes.py", line 516, in __init__
data = self._data_validator.validate_coerce(
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\_plotly_utils\basevalidators.py", line 2663, in validate_coerce
trace = self.get_trace_class(trace_type)(
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\graph_objs\_scattergeo.py", line 2138, in __init__
self["marker"] = _v
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\basedatatypes.py", line 4796, in __setitem__
self._set_compound_prop(prop, value)
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\basedatatypes.py", line 5207, in _set_compound_prop
val = validator.validate_coerce(val, skip_invalid=self._skip_invalid)
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\_plotly_utils\basevalidators.py", line 2450, in validate_coerce
v = self.data_class(v, skip_invalid=skip_invalid, _validate=_validate)
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\graph_objs\scattergeo\_marker.py", line 1412, in __init__
self["size"] = _v
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\basedatatypes.py", line 4804, in __setitem__
self._set_prop(prop, value)
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\basedatatypes.py", line 5148, in _set_prop
raise err
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\plotly\basedatatypes.py", line 5143, in _set_prop
val = validator.validate_coerce(val)
File "C:\Users\amanm\AppData\Local\Programs\Python\Python39\lib\site-packages\_plotly_utils\basevalidators.py", line 782, in validate_coerce
validators.py", line 293, in raise_invalid_elements
raise ValueError(
ValueError:
Invalid element(s) received for the 'size' property of scattergeo.marker
Invalid elements include: [-1.4000000000000001, -0.55, -1.5, -0.15, -5.05, -1.1, -4.1499999999999995, -3.85, -0.05, -1.0]
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发布于 2021-06-30 02:16:33
您发布的例外情况中最相关的部分在下面:
ValueError:
Invalid element(s) received for the 'size' property of scattergeo.marker
Invalid elements include: [-1.4000000000000001, -0.55, -1.5, -0.15, -5.05, -1.1, -4.1499999999999995, -3.85, -0.05, -1.0]
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这表明,在该数据源中的(吨)信息中,在键“特性”、子键“属性”、子键"mag“中,有些值是负值,而它们应该是0或更高才能被接受。
为了尝试从代码中获取内容并开始工作,您可以尝试将震级值的最小值设置为0,并希望没有其他超出预期的值:
#Plotting the earthquakes
data = [{
"type": "scattergeo",
"lon": longitude,
"lat": latitude,
"marker": {
"size": [max(5*mag, 0) for mag in mags], # change here
"color": mags,
"colorscale": "Viridis",
"reversescale": True,
"colorbar": {"title": "Magnitude"},
},
}]https://stackoverflow.com/questions/68187514
复制相似问题