Python制作全球地震散点图:JSON格式 您所在的位置:网站首页 蟒蛇python书 Python制作全球地震散点图:JSON格式

Python制作全球地震散点图:JSON格式

2023-09-03 20:34| 来源: 网络整理| 查看: 265

数据文件百度云链接:链接:https://pan.baidu.com/s/1SAIgm6pwrA0nRaSjz7nLoA  提取码:4lw2

文件eq_data_1_day_m1.json 记录了24小时内全球发生的所有不低于1级的地震。用记事本打开后,如下所示:

一、查看数据

我们先将加载这些数据并将其以易于阅读的方式显示处理。具体做法是将数据写入另一个文件,再打开并轻松地在数据中导航:

eq_explore_data.py 

import json # 探索数据的结构 filename = 'data/eq_data_1_day_m1.json' with open(filename) as f: all_eq_data = json.load(f) # 加载文件中的数据,将其存储到all_eq_data中,这是一个庞大的字典 # readable_file = 'data/readable_eq_data.json' # 创建一个新文件,以便将这些数据以易于阅读的方式写入其中 # with open(readable_file, 'w') as f: # json.dump(all_eq_data, f, indent=4) # 参数indent=4让dump()使用与数据结构匹配的缩进量来设置数据的格式 all_eq_dicts = all_eq_data['features'] print((len(all_eq_dicts)))

这时查看目录data,打开刚生成地文件readable_eq_data.json,我们发现该文件地开头:

readable_eq_data.json

{ "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": [ ...

这个文件的开头是一个键为"metadata"的片段,指出了这个数据文件是什么适合生成的,以及能够在网上的什么地方找到。它还包括适合人类阅读的标题以及文件中记录了多少次地震:在过去的24小时内,发生了158次地震。

再往下看,数据存储在一个与键"feayures"相关联的列表中。这个文件包含的是地震数据,因此列表的每个元素都对应一次地震。

readable_eq_data.json

... "features": [ { "type": "Feature", "properties": { "mag": 0.96, "place": "8km NE of Aguanga, CA", "time": 1550360775470, "updated": 1550360993593, "tz": -480, ... "geometry": { "type": "Point", "coordinates": [ -116.7941667, 33.4863333, 3.22 ] }, "id": "ci37532978" },

键"properties"关联到了与特定地震相关的大量信息。我们关心的主要是与键"mag"相关联的地震震级以及地震的标题,因为后者很好地概述了地震的震级和位置。

键"geometry"指出了地震在什么地方,我们需要根据这项信息将地震在散点图上标出来。在与键"coordinates"相关联的列表中,可找到地震发生位置的经度,纬度。

二、创建地震列表

eq_explore_data.py 

import json # 探索数据的结构 filename = 'data/eq_data_1_day_m1.json' with open(filename) as f: all_eq_data = json.load(f) # 加载文件中的数据,将其存储到all_eq_data中,这是一个庞大的字典 # readable_file = 'data/readable_eq_data.json' # 创建一个新文件,以便将这些数据以易于阅读的方式写入其中 # with open(readable_file, 'w') as f: # json.dump(all_eq_data, f, indent=4) # 参数indent=4让dump()使用与数据结构匹配的缩进量来设置数据的格式 all_eq_dicts = all_eq_data['features'] print((len(all_eq_dicts)))

这个文件记录了158次地震

158 三、提取震级

有了包含所有地震数据的列表后,就可遍历这个列表,从中提取所需的数据。下面来提取每次地震的震级。

eq_explore_data.py 

import json # 探索数据的结构 filename = 'data/eq_data_1_day_m1.json' with open(filename) as f: all_eq_data = json.load(f) # 加载文件中的数据,将其存储到all_eq_data中,这是一个庞大的字典 # readable_file = 'data/readable_eq_data.json' # 创建一个新文件,以便将这些数据以易于阅读的方式写入其中 # with open(readable_file, 'w') as f: # json.dump(all_eq_data, f, indent=4) # 参数indent=4让dump()使用与数据结构匹配的缩进量来设置数据的格式 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])

我们依次读取每次地震震级,存在列表mags中。为确定提取的数据是否正确,打印前10次地震的震级:

[0.96, 1.2, 4.3, 3.6, 2.1, 4, 1.06, 2.3, 4.9, 1.8] 四、提取位置数据

位置数据存储在”geometry" 键下。在”geometry" 键关联的字典中,有一个键"coordinates",它关联到一个列表。而列表中的前两个值为经度和纬度。下面演示如何提取位置数据

eq_explore_data.py

import json # 探索数据的结构 filename = 'data/eq_data_1_day_m1.json' with open(filename) as f: all_eq_data = json.load(f) # 加载文件中的数据,将其存储到all_eq_data中,这是一个庞大的字典 # readable_file = 'data/readable_eq_data.json' # 创建一个新文件,以便将这些数据以易于阅读的方式写入其中 # with open(readable_file, 'w') as f: # json.dump(all_eq_data, f, indent=4) # 参数indent=4让dump()使用与数据结构匹配的缩进量来设置数据的格式 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[:2]) print(lons[:5]) print(lats[:5])

打印前5个经度和纬度时,输出表明提取的数据是正确的

[0.96, 1.2, 4.3, 3.6, 2.1, 4, 1.06, 2.3, 4.9, 1.8] ['M 1.0 - 8km NE of Aguanga, CA', 'M 1.2 - 11km NNE of North Nenana, Alaska'] [-116.7941667, -148.9865, -74.2343, -161.6801, -118.5316667] [33.4863333, 64.6673, -12.1025, 54.2232, 35.3098333] 五、绘制震级散点图

有了前面提取的数据,我们就可以绘制可视化图了。首先要实现一个简单的震级散点图,在确保显示的信息无误后,我们再将注意力转向样式和外观方面。新建一个eq_world_map.py文件,绘制初始散点图

eq_world_map.py

import plotly.express as px from eq_explore_data import lons,lats fig = px.scatter( x=lons, y=lats, labels={'x': '纬度', 'y': '经度'}, range_x=[-200, 200], range_y=[-90, 90], width=800, height=800, title='全球地震散点图' ) fig.write_html('global_earthquakes.html') fig.show()

局部效果如图所示:

 接下来对这幅图做大量修改,使其更有意义

六、定制标记的尺寸

当前的散点图显示了每次地震的位置,但没有指出震级,我们要让观察者迅速获悉最严重的地震发生再什么地方,为此,根据地震的震级设置其标记的尺寸

eq_world_map.py

import plotly.express as px from eq_explore_data import lons, lats, mags fig = px.scatter( x=lons, y=lats, labels={'x': '纬度', 'y': '经度'}, range_x=[-200, 200], range_y=[-90, 90], width=800, height=800, title='全球地震散点图', size=mags, size_max=10, ) fig.write_html('global_earthquakes.html') fig.show()

运行结构如图:

 七、定制标记的颜色

接下来将使用eq_data_30_day_m1.json数据文件,它包含30天内的地震数据。

下面演示如何使用渐变来呈现地震震级、

eq_world_map.py

import plotly.express as px from eq_explore_data import lons, lats, mags, titles fig = px.scatter( x=lons, y=lats, labels={'x': '纬度', 'y': '经度'}, range_x=[-200, 200], range_y=[-90, 90], width=800, height=800, title='全球地震散点图', size=mags, size_max=10, color=mags, hover_name=titles ) fig.write_html('global_earthquakes.html') fig.show()

运行结果:

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有