python 下載數(shù)據(jù)json 2021-02-27

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 = '位置',? ? #添加鼠標指向時顯示的文本

? ??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容