值错误 / UnicodeDecodeError
ValueError / UnicodeDecodeError
所以我想创建一个程序,在图表上显示过去 30 天的地震,我一直在使用这个 GeoJSON 数据:
[https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson][1]
这是我的程序。我不太确定该怎么做。之前我被告知 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
您发布的异常最相关的部分在底部:
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
这表明在该数据源的(大量)信息中,键“features”、子键“properties”、子键“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"},
},
}]
所以我想创建一个程序,在图表上显示过去 30 天的地震,我一直在使用这个 GeoJSON 数据:
[https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson][1]
这是我的程序。我不太确定该怎么做。之前我被告知 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
您发布的异常最相关的部分在底部:
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
这表明在该数据源的(大量)信息中,键“features”、子键“properties”、子键“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"},
},
}]