Plotly - 如何从 json 文件在地图上绘制一条线?
Plotly - How do I plot a line on a map from a json file?
我正在尝试从 json 文件中以 plotly 的方式在地图上绘制一条线,但不断收到以下错误。 plotly 网站上的所有示例都使用 .csv 文件。但是,我想使用 json 文件。任何帮助将不胜感激。
Error: No valid mapbox style found, please set mapbox.style
to one of:
open-street-map, white-bg, carto-positron, carto-darkmatter, stamen-terrain, stamen-toner, stamen-watercolor
or register a Mapbox access token to use a Mapbox-served style.
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()
Json:
{
"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)
我正在尝试从 json 文件中以 plotly 的方式在地图上绘制一条线,但不断收到以下错误。 plotly 网站上的所有示例都使用 .csv 文件。但是,我想使用 json 文件。任何帮助将不胜感激。
Error: No valid mapbox style found, please set
mapbox.style
to one of: open-street-map, white-bg, carto-positron, carto-darkmatter, stamen-terrain, stamen-toner, stamen-watercolor or register a Mapbox access token to use a Mapbox-served style.
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()
Json:
{
"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)