plotly.express 绘图 处理Json文件

plotly.express 绘制 地震点

复制代码
import plotly.express as px
import pandas as pd
import json

filename = 'data/eq_data_1_day_m1.json'
with open(filename) as f:
    # 函数json.load() 将数据转换为Python能够处理的格式,这里是一个庞大的字典  将其存储到all_eq_data中
    all_eq_data = json.load(f)

# 提取与键'features'相关联的数据,并将其存储到all_eq_dicts中
all_eq_dicts = all_eq_data['features']

mags, titles, lons, lats = [], [], [], []  # msgs震级,longs经度,lats纬度
for eq_dict in all_eq_dicts:
    msg = eq_dict["properties"]["mag"]  # 将properties字典中的msg键值 逐个添加到msgs[]列表中
    title = eq_dict["properties"]["title"]
    lon = eq_dict["geometry"]["coordinates"][0]  # 提取索引0的第一个值,即经度
    lat = eq_dict["geometry"]["coordinates"][1]  # 提取索引1的第二个值,即纬度

    mags.append(msg)
    titles.append(title)
    lons.append(lon)
    lats.append(lat)


# 调用px.scatter函数配置参数创建一个fig实例,分别设置
# x轴为经度[范围是[-200, 200](扩大空间,以便完整显示东西经180°附近的地震散点)]、
# y轴为纬度[范围是[-90, 90]],
# 设置散点图显示的宽度和高度均为800像素,并设置标题为“全球地震散点图”
fig = px.scatter(
    x=lons,
    y=lats,
    labels={"x": "经度", "y": "纬度"},
    range_x=[-200, 200],  # 分别设置轴为经度[范围是[-200, 200]
    range_y=[-90, 90],
    width=800,
    height=800,
    title="全球地震散点图",
)
fig.write_html("global_earthquakes.html")
fig.show()
复制代码

python3.x  zip()函数的适用

复制代码
>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b)     # 返回一个对象
>>> zipped
<zip object at 0x103abc288>
>>> list(zipped)  # list() 转换为列表
[(1, 4), (2, 5), (3, 6)]
>>> list(zip(a,c))              # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
 
>>> a1, a2 = zip(*zip(a,b))          # 与 zip 相反,zip(*) 可理解为解压,返回二维矩阵式
>>> list(a1)
[1, 2, 3]
>>> list(a2)
[4, 5, 6]
复制代码

 

 pandas 来处理源数据

复制代码
import plotly.express as px
import pandas as pd
import json


# filename = 'data/eq_data_1_day_m1.json'
filename = 'data/eq_data_30_day_m1.json'
with open(filename) as f:
    # 函数json.load() 将数据转换为Python能够处理的格式,这里是一个庞大的字典  将其存储到all_eq_data中
    all_eq_data = json.load(f)

# 提取与键'features'相关联的数据,并将其存储到all_eq_dicts中
all_eq_dicts = all_eq_data['features']

mags, titles, lons, lats = [], [], [], []  # msgs震级,longs经度,lats纬度
for eq_dict in all_eq_dicts:
    msg = eq_dict["properties"]["mag"]  # 将properties字典中的msg键值 逐个添加到msgs[]列表中
    title = eq_dict["properties"]["title"]
    lon = eq_dict["geometry"]["coordinates"][0]  # 提取索引0的第一个值,即经度
    lat = eq_dict["geometry"]["coordinates"][1]  # 提取索引1的第二个值,即纬度

    mags.append(msg)
    titles.append(title)
    lons.append(lon)
    lats.append(lat)

data = pd.DataFrame(data=zip(lons, lats, titles, mags), columns=["经度", "纬度", "位置", "震级"])
data.head(10)
# 调用px.scatter函数配置参数创建一个fig实例,分别设置
# x轴为经度[范围是[-200, 200](扩大空间,以便完整显示东西经180°附近的地震散点)]、
# y轴为纬度[范围是[-90, 90]],
# 设置散点图显示的宽度和高度均为800像素,并设置标题为“全球地震散点图”
fig = px.scatter(
    data,
    x="经度",
    y="纬度",
    range_x=[-200, 200],  # 分别设置轴为经度[范围是[-200, 200]
    range_y=[-90, 90],
    width=1200,
    height=800,
    title="全球地震散点图",
    size="震级",
    size_max=10,
    color="震级",
    hover_name="位置"
)
fig.write_html("global_earthquakes.html")
fig.show()
复制代码

 

posted @   茶叶蛋蛋  阅读(244)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示