[英]How do I know if the code below is correct?
From my previous request, I created an empty list (clean_mag) and then loop through the mags list, and append the positive numbers to the clean_mag(list) because I realized that the negative numbers was the reason why I was getting a ValueError in my previous code.根据我之前的请求,我创建了一个空列表(clean_mag),然后遍历 mags 列表,append 将正数添加到 clean_mag(list),因为我意识到负数是我在我的以前的代码。 Although the code worked, but I would like to know if is correct, but as a self-learner, I do not know how else to cross check my code for validity.
虽然代码有效,但我想知道是否正确,但作为自学者,我不知道如何交叉检查我的代码的有效性。
How do I know that I am not missing anything out?我怎么知道我没有遗漏任何东西? I would like to know if there's a better way to go through the mag list, and then work with the positive numbers without allowing the negative numbers to blow up the code.
我想知道是否有更好的方法通过 mag 列表来 go,然后使用正数而不让负数破坏代码。 I thought about using try/except but it didn't work.
我考虑过使用 try/except 但它没有用。
import json
from plotly.graph_objs import Scattergeo, Layout
from plotly import offline
# Get a JSON file.
filename = 'earthquake_data/seven_days_earthquake.json'
with open(filename, encoding='utf-8') as f:
eq_data = json.load(f)
# Create a readable file from the loaded json file above.
readable_file = 'earthquake_data/readable_eq_data.json'
with open(readable_file, 'w') as f:
json.dump(eq_data, f, indent=4)
# Exploring the structure of the data.
title = eq_data["metadata"]["title"]
all_eq_dicts = eq_data["features"]
mags, lons, = [], []
lats, hover_texts = [], []
for eq_dict in all_eq_dicts:
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"])
clean_mags = []
for item in mags:
if item < 0:
pass
else:
clean_mags.append(item)
# Map the earthquakes.
data = [{
'type': 'scattergeo',
'lon': lons,
'lat': lats,
'text': hover_texts,
# Outline marks in the map
'marker': {
'size':[5*mag for mag in clean_mags],
'color': mags,
'colorscale': 'Ylorrd',
'reversescale': True,
'colorbar': {'title': 'Magnitude'},
},
}]
# One-week Earthquake data output.
my_layout = Layout(title = f'{title}')
fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='sevenday_quakes.html')
Since you did not present any data, I got similar data from here .由于您没有提供任何数据,我从 这里得到了类似的数据。 If the magnitude is greater than or equal to 0, the latitude, longitude, and name will also be listed.
如果幅度大于等于0,也会列出纬度、经度和名称。 This answer may differ from the data the questioner has, and the method of answering may not be valid.
此答案可能与提问者所拥有的数据不同,并且回答的方法可能无效。 If this is the case, please comment.
如果是这种情况,请发表评论。
from urllib import request
import json
url='https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson'
with request.urlopen(url) as f:
eq_data = json.load(f)
from urllib import request
import json
url='https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson'
with request.urlopen(url) as f:
eq_data = json.load(f)
import plotly.graph_objs as go
fig = go.Figure()
fig.add_trace(go.Scattergeo(
lon=lons,
lat=lats,
text=hover_texts,
marker=dict(
size=[x*3 for x in mags],
color=mags,
colorscale='Ylorrd',
reversescale=False,
colorbar=dict(title='Magnitude'),
)
))
fig.update_layout(
autosize=True,
height=600,
title_text='All Earthquakes(Last 7 days)',
showlegend=False,
geo = dict(
landcolor='rgb(217, 217, 217)',
)
)
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.