如何用 pandas 绘制散点图框?
How to plot a plotly scattermapbox with pandas?
我正在尝试使用 geojson 文件在散点图框上绘制 lat/lon 点。我将 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
关于如何正确循环数据框并绘制这些线有什么建议吗?
好的。所以使用 pandas 和 plotly scattermapbox 是一件令人头疼的事情。这就是我如何从 geojson 文件中提取坐标,以便在 go.Scattermapbox
的 lat
和 lon
参数中绘制。
import plotly.graph_objects as go
import json
geojson = json.load(open("Data/lanes.geojson"))
points = []
for feature in geojson['features']:
if feature['geometry']['type'] == 'Polygon':
points.extend(feature['geometry']['coordinates'][0])
points.append([None, None]) # mark the end of a polygon
elif feature['geometry']['type'] == 'MultiPolygon':
for polyg in feature['geometry']['coordinates']:
points.extend(polyg[0])
points.append([None, None]) #end of polygon
elif feature['geometry']['type'] == 'MultiLineString':
points.extend(feature['geometry']['coordinates'])
points.append([None, None])
else: pass
lons, lats = zip(*points)
fig = go.Figure(
go.Scattermapbox(
name='High Comfort Bike Lane',
hoverinfo='name',
lat=lats,
lon=lons,
mode="lines",
line=dict(width=3, color="#F00")
)
我正在尝试使用 geojson 文件在散点图框上绘制 lat/lon 点。我将 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
关于如何正确循环数据框并绘制这些线有什么建议吗?
好的。所以使用 pandas 和 plotly scattermapbox 是一件令人头疼的事情。这就是我如何从 geojson 文件中提取坐标,以便在 go.Scattermapbox
的 lat
和 lon
参数中绘制。
import plotly.graph_objects as go
import json
geojson = json.load(open("Data/lanes.geojson"))
points = []
for feature in geojson['features']:
if feature['geometry']['type'] == 'Polygon':
points.extend(feature['geometry']['coordinates'][0])
points.append([None, None]) # mark the end of a polygon
elif feature['geometry']['type'] == 'MultiPolygon':
for polyg in feature['geometry']['coordinates']:
points.extend(polyg[0])
points.append([None, None]) #end of polygon
elif feature['geometry']['type'] == 'MultiLineString':
points.extend(feature['geometry']['coordinates'])
points.append([None, None])
else: pass
lons, lats = zip(*points)
fig = go.Figure(
go.Scattermapbox(
name='High Comfort Bike Lane',
hoverinfo='name',
lat=lats,
lon=lons,
mode="lines",
line=dict(width=3, color="#F00")
)