简体   繁体   中英

How to plot a plotly scattermapbox with pandas?

I'm trying to plot lat/lon points on a scattermapbox with a geojson file. I threw the geojson file in a panada's data frame and made an empty list for lat and lon since thats all scattermapbox accepts. It didn't turn out as expected.

在此处输入图片说明

Python Plotly:

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 Dataframe:

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

Any suggestions on how to loop through the data frame correctly and plot these lines?

Ok. So using pandas with plotly scattermapbox is a headache. This is how I extracted the coordinates from the geojson file to be plotted in the lat and lon parameters of go.Scattermapbox .

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")
        ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM