Plotly – How do I plot a line on a map from a json file?
Question:
I’m trying to plot a line on a map in plotly from a json file and keep receiving the following error. All the examples on the plotly website use a .csv file. However, I’d like to use a json file. Any help would be greatly appreciated.
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
]
]
}
}
]
}
Answers:
You are not reading the json file correctly. You’re trying to read the "fcRailroad.json" as json. And it is obviously not valid json. This is how you load the content from the file:
with open('fcRailroad.json') as json_file:
fcRailroad = json.load(json_file)
I’m trying to plot a line on a map in plotly from a json file and keep receiving the following error. All the examples on the plotly website use a .csv file. However, I’d like to use a json file. Any help would be greatly appreciated.
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
]
]
}
}
]
}
You are not reading the json file correctly. You’re trying to read the "fcRailroad.json" as json. And it is obviously not valid json. This is how you load the content from the file:
with open('fcRailroad.json') as json_file:
fcRailroad = json.load(json_file)