0

I am currently trying to plot lat long points with connected edges from a map api to a summarizing graph. I'm currently using plotly to do this from python, but my issue is that the horizontal axis 'zooms' or stretches, as documented here resulting in something like this: enter image description here

Can anyone recommend a 2D vector plotting library in js or python to accomplish this without stretching?

2 Answers 2

1
  • you have been quite abstract defining your data. Hence I used simple to use osmnx which provides simple way to source nodes and edges
  • edges will be LineString, plotly wants a list of lat and lon, hence extraction of co-ordinates and flattening
  • have demonstrated
    1. line trace, clearly this is stretched (as expected) because it does not take into account geometric projections onto linear space
    2. line mapbox trace
    3. folium

source some points and connected edges

import osmnx as ox
import plotly.express as px
import numpy as np

# get some edges
gdf = ox.geocode_to_gdf('Topsham, Devon')
G = ox.graph_from_polygon(gdf.loc[0,"geometry"], network_type='drive')
gdf_nodes, gdf_edges = ox.graph_to_gdfs(G)

plotly lines

  • these are stretched
# plotly lines are arrays of points with each line separated by None
px.line(
    x=np.concatenate(
        gdf_edges["geometry"].apply(lambda g: [c[1] for c in g.coords] + [None]).values
    ),
    y=np.concatenate(
        gdf_edges["geometry"].apply(lambda g: [c[0] for c in g.coords] + [None]).values
    ),
)

enter image description here

plotly mapbox

  • no stretching
px.line_mapbox(
    lat=np.concatenate(
        gdf_edges["geometry"].apply(lambda g: [c[1] for c in g.coords] + [None]).values
    ),
    lon=np.concatenate(
        gdf_edges["geometry"].apply(lambda g: [c[0] for c in g.coords] + [None]).values
    ),
).update_layout(mapbox={"style":"carto-positron", "zoom":13})

enter image description here

folium (using geopandas)

gdf_edges.explore()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Can annotations be added for each subline and makrers for each point in the line_mapbox() figure?
1

I used the NetworkX and matplotlib.pyplot library.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.