John
John

Reputation: 329

Plotting a geopandas dataframe geometry with plotly

In this notebook I plot the geometry of a seismic survey design (first figure), I want to make it interactive with plotly but I've failed.

I found this question and tried the solution. First converting my geodataframe to GeoJson, It looks like this:

{"type": "FeatureCollection", "features": [
    {"id": "0", "type": "Feature", "properties": {"SID": 0, "station": "r"}, "geometry": {"type": "Point", "coordinates": [574950.0, 4710050.0]}},
    {"id": "1", "type": "Feature", "properties": {"SID": 1, "station": "r"}, "geometry": {"type": "Point", "coordinates": [575050.0, 4710050.0]}},
}

And then plot it

go.Figure(
    [
        go.Scatter(
            **{
                "x": [p[0] for p in f["geometry"]["coordinates"][0]],
                "y": [p[1] for p in f["geometry"]["coordinates"][0]],
                "fill": "toself",
                "name": f["properties"]["id"],
            }
        )
        for f in geodata["features"]
    ]
).update_layout(height=200, width=200, showlegend=False, margin={"l":0,"r":0,"t":0,"b":0})

I got the next error TypeError: string indices must be integers

How can my problem be solved?

Upvotes: 0

Views: 677

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31146

It is far simpler given it's just points.

straight scatter

import plotly.express as px

px.scatter(survey, x=survey.geometry.x, y=survey.geometry.y, color="station")

enter image description here

scatter with map layer as well

  • need to consider CRS projection
survey_ = survey.to_crs("epsg:4386")
px.scatter_mapbox(
    survey_, lat=survey_.geometry.y, lon=survey_.geometry.x, color="station"
).update_layout(mapbox={"style": "carto-positron", "zoom": 12})

enter image description here

Upvotes: 1

Related Questions