import json
#探索數(shù)據(jù)的結(jié)構(gòu)
filename = 'data/1.json'
with open(filename) as f:
? ? all_eq_data = json.load(f)? ? #存儲進去一個json數(shù)據(jù)對象
'''
readable_file = 'data/readable_eq_data.json'? ? #創(chuàng)建一個文件對象
with open(readable_file,'w') as f:
? ? json.dump(all_eq_data,f,indent = 4)? ? #接受一個json數(shù)據(jù)對象和文件對象? ? indent縮進
'''
all_eq_dicts = all_eq_data['features']? ? #提取鍵"features"數(shù)據(jù)并儲存
mags,titles,lons,lats= [],[],[],[]
for eq_dict in all_eq_dicts:
? ? mag = eq_dict['properties']['mag']? ? #每次地震震級存儲在'properties'部分的'mag'下
? ? title = eq_dict['properties']['title']? ? #存儲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(len(all_eq_dicts))? ? #提取所有地震的次數(shù)
print(titles[:2])
print(lons[:5])
print(lats[:5])
繪制震級散點圖:
import plotly.express as px
fig = px.scatter(
? ? x = lons,
? ? y = lats,
? ? labels = {'x':'經(jīng)度','y':'緯度'},
? ? range_x = [-200,200]
? ? range_y = [-90,90]
? ? width = 800,
? ? height = 800,
????title = '全球地震散點圖'
)
fig.write_html('global_earthquakes.html')? ? #保存文件
fig.show()? ? #顯示
另一種指定圖標數(shù)據(jù)的方式:
import pandas as pd
data = pd.DataFrame(
? ? data = zip(lons,lats,titles,mags),columns = ['經(jīng)度','緯度','位置','震級']? ? #封裝數(shù)據(jù)
)
data.head()
然后參數(shù)配置方式可以從:
? ? x = lons,
? ? y = lats,
? ? labels = {'x':'經(jīng)度','y':'緯度'},
變更為:
????data,
? ? x = '經(jīng)度'
? ? y = '緯度'
? ? ……
? ? size = '震級'硼一,
? ? size_max = 10,? ? #默認20
? ? color = '震級',? ? #標記顏色 藍<紅<黃
? ? hover_name = '位置',? ? #添加鼠標指向時顯示的文本
? ??