我正在尝试在带有geojson文件的散点图框上绘制纬度/经度点。我将geojson文件放入了panada的数据框中,并为lat和lon制作了一个空列表,因为那是所有scattermapbox都能接受的。结果未如预期。
Python密谋:
import plotly.graph_objects as go
import pandas as pd
from pandas.io.json import json_normalize
import json
geojson = json.load(open("Data/High Comfort Bike Lanes.geojson"))
geojson = json_normalize(geojson['features'], sep="_")
lon_comfortBike = []
lat_comfortBike = []
for l1 in geojson['geometry_coordinates']:
for l2 in l1:
for l3 in l2:
lon_comfortBike.append(l2[0])
lat_comfortBike.append(l2[1])
fig = go.Figure(
data=[
go.Scattermapbox(
name='High Comfort Bike Route',
hoverinfo='name',
lat=lat_comfortBike,
lon=lon_comfortBike,
mode="lines",
line=dict(width=3, color="#F00")
]
)
mapLegend = go.layout.Legend(
x=0,
y=1,
traceorder="normal",
font=dict(
family="sans-serif",
size=12,
color="black"
),
bgcolor="LightSteelBlue",
bordercolor="Black",
borderwidth=2
)
fig.update_layout(
showlegend=True,
legend=mapLegend,
margin={"r":0,"t":0,"l":0,"b":0},
mapbox=go.layout.Mapbox(
style="stamen-terrain",
zoom=12,
center_lat = 40.55,
center_lon = -105.08,
)
)
fig.show()
Python数据框:
0 [[[-105.077274, 40.514625], [-105.077005, 40.5...
1 [[[-105.024284, 40.509791], [-105.024274, 40.5...
2 [[[-105.087928, 40.578187], [-105.087939, 40.5...
3 [[[-105.11976, 40.589318], [-105.11977, 40.587...
4 [[[-105.083718, 40.568761], [-105.08459, 40.56...
...
995 [[[-105.05362, 40.525161], [-105.053607, 40.52...
996 [[[-105.030003, 40.62114], [-105.030012, 40.62...
997 [[[-105.123316, 40.560645], [-105.123353, 40.5...
998 [[[-105.070162, 40.580083], [-105.070175, 40.5...
999 [[[-105.120617, 40.560044], [-105.120637, 40.5...
Name: geometry_coordinates, Length: 1000, dtype: object
关于如何正确遍历数据框并绘制这些线的任何建议?
答案 0 :(得分:0)
好的。因此,将熊猫与plotly scattermapbox一起使用是一件令人头疼的事情。这就是我从geojson文件中提取坐标以在lat
的{{1}}和lon
参数中进行绘制的方式。
go.Scattermapbox