import json
filename='D:\matplotlib\eq_data_1_day_m1 (1).json'
with open(filename) as f:
all_eq_data=json.load(f)
readable_file='eq_data_1_day_m1.json'
with open(readable_file,'w') as f:
json.dump(all_eq_data,f,indent=4)
这里注意将filename的相对路径直接复制,以免出现找不到文件之类的错误;
效果为:
{
"type": "FeatureCollection",
"metadata": {
"generated": 1550361461000,
"url": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_day.geojson",
"title": "USGS Magnitude 1.0+ Earthquakes, Past Day",
"status": 200,
"api": "1.7.0",
"count": 158
},
"features": [
{
"type": "Feature",
"properties": {
"mag": 0.96,
"place": "8km NE of Aguanga, CA",
"time": 1550360775470,
"updated": 1550360993593,
"tz": -480,
"url": "https://earthquake.usgs.gov/earthquakes/eventpage/ci37532978",
"detail": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ci37532978.geojson",
"felt": null,
"cdi": null,
"mmi": null,
"alert": null,
"status": "automatic",
"tsunami": 0,
"sig": 14,
"net": "ci",
"code": "37532978",
"ids": ",ci37532978,",
"sources": ",ci,",
"types": ",geoserve,nearby-cities,origin,phase-data,",
"nst": 32,
"dmin": 0.02648,
"rms": 0.15,
"gap": 37,
"magType": "ml",
"type": "earthquake",
"title": "M 1.0 - 8km NE of Aguanga, CA"
},
下面提取数据:
import json
filename='D:\matplotlib\eq_data_1_day_m1 (1).json'
with open(filename) as f:
all_eq_data=json.load(f)
all_eq_dicts = all_eq_data['features']
mags = []
for eq_dict in all_eq_dicts:
mag = eq_dict['properties']['mag']
mags.append(mag)
print(mags[:10])
[0.96, 1.2, 4.3, 3.6, 2.1, 4, 1.06, 2.3, 4.9, 1.8]
mags = []
for eq_dict in all_eq_dicts:
mag = eq_dict['properties']['mag']
mags.append(mag)
这一段提取properties下面的mag数据
下面提取多个数据:
import json
filename='D:\matplotlib\eq_data_1_day_m1 (1).json'
with open(filename) as f:
all_eq_data=json.load(f)
all_eq_dicts = all_eq_data['features']
mags,titles,lons,lats = [],[],[],[]
for eq_dict in all_eq_dicts:
mag = eq_dict['properties']['mag']
title = eq_dict['properties']['title']
lon = eq_dict['geometry']['coordinates'][0]
lat = eq_dict['geometry']['coordinates'][1]
mags.append(mag)
titles.append(title)
lons.append(lon)
lats.append(lat)
print(mags[:10])
print(titles[:10])
print(lons[:10])
print(lats[:10])
下面绘制散点图:
import plotly.express as px
import equ_explore_data
import json
fig = px.scatter(
x=lons,
y=lats,
labels={'x':'Jingdu','y':'Weidu'},
range_x=[-200,200],
range_y=[-90,90],
width=800,
height=800,
title='Sandiantu'
)
fig.write_html('global_earthquakes.html')
fig.show()