You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python处理地震JSON数据:移除负值解决Plotly scattergeo size报错

问题原因

报错是由于Plotly的scattergeo.marker.size属性仅支持≥0的数值,而USGS公开的地震数据集里本身包含震级为负的极微小地震记录,计算5*mag时产生的负值不符合参数校验规则。

解决方法

你可以在遍历提取地震数据时增加过滤条件,直接剔除震级为负的条目,修改后的代码如下:

# Get all the magnitudes.
mags, lons, lats, hover_texts = [], [], [], []
for eq_dicts in all_eq_dicts:
    mag = eq_dicts['properties']['mag']
    # 过滤震级小于0的条目
    if mag >= 0:
        mags.append(mag)
        lons.append(eq_dicts['geometry']['coordinates'][0])
        lats.append(eq_dicts['geometry']['coordinates'][1])
        hover_texts.append(eq_dicts['properties']['title'])

可选方案:保留所有条目,替换负震级

如果你不想删除数据,也可以直接对负震级做截断处理,把负震级映射为0,仅需修改size的计算逻辑即可,无需调整数据提取部分:

'marker': {
    # 把负震级转为0再计算大小
    'size': [5*max(mag, 0) for mag in mags],
    'color': mags,
    'colorscale': 'Viridis',
    'reversescale': True,
    'colorbar': {'title': 'Magnitude'},
},

(可选)保存过滤后的JSON文件

如果需要后续直接使用过滤后的数据,可以新增以下代码,将剔除负震级后的数据集保存为新的JSON文件:

# 生成过滤后的数据集
filtered_features = [eq for eq in all_eq_dicts if eq['properties']['mag'] >= 0]
filtered_eq_data = all_eq_data.copy()
filtered_eq_data['features'] = filtered_features

# 保存为新文件
with open('data/filtered_7_day_eq.json', 'w', encoding='utf-8') as f:
    json.dump(filtered_eq_data, f, indent=4)

内容的提问来源于stack exchange,提问作者Mike

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 06:18:00