簡體   English   中英

Plotly - 如何從 json 文件在地圖上繪制一條線?

[英]Plotly - How do I plot a line on a map from a json file?

我正在嘗試從 json 文件中以 plotly 在地圖上繪制一條線,並不斷收到以下錯誤。 plotly 網站上的所有示例都使用 .csv 文件。 但是,我想使用 json 文件。 任何幫助將不勝感激。

錯誤:未找到有效的 mapbox 樣式,請將mapbox.style設置為以下之一:open-street-map、white-bg、carto-positron、carto-darkmatter、stamen-terrain、stamen-toner、stamen-watercolor 或注冊 Mapbox訪問令牌以使用 Mapbox 服務的樣式。

Python:

import plotly.graph_objects as go
import pandas as pd
import json

with open('fcRailroad.geojson') as json_file:
    fcRailroad = json.load(json_file)

fig = go.Figure(go.Scattermapbox())

fig.update_layout(mapbox_style="stamen-terrain", 
                mapbox_zoom=10, 
                mapbox_center_lat = 40.58,
                mapbox_center_lon = -105.08,
                margin={"r":0,"t":0,"l":0,"b":0},
                mapbox=go.layout.Mapbox(
                    layers=[{
                        'sourcetype': 'geojson',
                        'source': fcRailroad,
                        'type': 'line',
                    }]
                ))

fig.show()

傑森:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        -825.0913953781128,
                        40.49348616373978
                    ],
                    [
                        -825.0906443595885,
                        40.49508532104079
                    ],
                    [
                        -825.0863313674927,
                        40.502411585011934
                    ]
                ]
            }
        }
    ]
}

您沒有正確讀取 json 文件。 您正在嘗試將“fcRailroad.json”讀取為 json。 它顯然不是有效的 json。 這是從文件加載內容的方式:

with open('fcRailroad.json') as json_file:
    fcRailroad = json.load(json_file)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM