【问题标题】:Scattergeo of Earthquake jsondata using plotly in python在python中使用plotly的地震jsondata的Scattergeo
【发布时间】:2021-08-29 08:04:56
【问题描述】:

我想使用 Plotly 在世界地图上可视化地震发生区域。我想通过访问经度和纬度来提供世界地图上大致位置的描述。 我还想看看世界上最严重的地震发生在哪里,我试图通过根据震级改变标记的大小来显示地震的严重程度。

我现在不想拥有 pandas 模块,因为我还没有学过它们。我不知道代码有什么问题,它在浏览器上显示为空白。我处于初学者阶段,所以请尽可能简单地建议我。

代码:

import json
from plotly.graph_objs import Scattergeo, Layout
from plotly import offline

# Explore the structure of the data.
filename = 'Datasets/eq_data_30_day_m1.json'
with open(filename) as f:
    all_eq_data = json.load(f)

# Extracting all the important key features.
all_eq_dicts = all_eq_data['features']

# Extracting the magnitude and the locations.
mags, lons, lats, hover_texts = [], [], [], []
for eq_dict in all_eq_dicts:
    mag = eq_dict['properties']['mag']
    lon = eq_dict['geometry']['coordinates'][0]
    lat = eq_dict['geometry']['coordinates'][1]
    title = eq_dict['properties']['title']
    mags.append(mag)
    lons.append(lon)
    lats.append(lat)
    hover_texts.append(title)

# Map the earthquakes.
data = [{
    'type': 'scattergeo',
    'lon': lons,
    'lat': lats,
    'text': hover_texts,
    'marker': {
        'size': [5*mag for mag in mags],
        'color': mags,
        'colorscale': 'Viridis',
        'reversescale': True,
        'colorbar': {'title': 'Magnitude'},
    },
}]

my_layout = Layout(title='Global Earthquakes')

fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='global_earthquakes.html')

【问题讨论】:

  • 抱歉给您添麻烦了,这实际上是我在这里的第一个问题,感谢您指出。

标签: python plotly data-analysis


【解决方案1】:
  • pandas / geopandas 无需解析 GEOJSON
  • 将您的代码用于等效而不使用 pandas
    1. 设置以下大小标记"sizeref": 0.02025, "symbol": "circle", "sizemode": "area",
    2. 更改了布局以将边距设置为零
  • 发现数据在这里https://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php 无需下载和打开文件。只需使用请求
import requests
import geopandas as gpd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px

res = requests.get("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_hour.geojson")

geo_df = gpd.GeoDataFrame.from_features(res.json())
px.scatter_geo(
    geo_df,
    lat=geo_df.geometry.y,
    lon=geo_df.geometry.x,
    size="mag",
    color="mag",
    hover_name="place",
)

不使用 geopandas

import json, requests
from plotly.graph_objs import Scattergeo, Layout
from plotly import offline
import plotly.graph_objects as go

# Explore the structure of the data.
# filename = 'Datasets/eq_data_30_day_m1.json'
# with open(filename) as f:
#     all_eq_data = json.load(f)
all_eq_data = requests.get(
    "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.geojson"
).json()

# Extracting all the important key features.
all_eq_dicts = all_eq_data["features"]

# Extracting the magnitude and the locations.
mags, lons, lats, hover_texts = [], [], [], []
for eq_dict in all_eq_dicts:
    mag = eq_dict["properties"]["mag"]
    lon = eq_dict["geometry"]["coordinates"][0]
    lat = eq_dict["geometry"]["coordinates"][1]
    title = eq_dict["properties"]["title"]
    mags.append(mag)
    lons.append(lon)
    lats.append(lat)
    hover_texts.append(title)

# Map the earthquakes.
data = [
    {
        "type": "scattergeo",
        "lon": lons,
        "lat": lats,
        "text": hover_texts,
        "marker": {
            "size": mags,
            "sizeref": 0.02025,
            "symbol": "circle",
            "sizemode": "area",
            "color": mags,
            "colorscale": "Viridis",
            "reversescale": True,
            "colorbar": {"title": "Magnitude"},
        },
    }
]

my_layout = Layout(title="Global Earthquakes",margin={"l":0,"r":0,"t":0,"b":0})

fig = {"data": data, "layout": my_layout}

offline.plot(fig, filename='global_earthquakes.html')

【讨论】:

  • 谢谢,rob 的帮助,但我不想使用 Plotly express,因为它要我安装我现在不想要的 pandas 包,所以请建议我另一种方法这是我的代码,请检查它们是否正确或不让我知道。再次感谢
  • 我已经更新了答案,对您的设置代码进行了非常小的调整。我无权访问您的文件系统....所以直接使用 requests 获取数据
  • 非常感谢 Rob 的宝贵时间和帮助。我真的很感激。
猜你喜欢
  • 2017-04-28
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
相关资源
最近更新 更多