Jonathon Machinski
Jonathon Machinski

Reputation: 75

Alternatives to plotly.graph_objects for plotting 2D lines (python or js)

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?

Upvotes: 0

Views: 347

Answers (2)

Rob Raymond
Rob Raymond

Reputation: 31226

  • 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

Upvotes: 1

PaulGilbert
PaulGilbert

Reputation: 46

I used the NetworkX and matplotlib.pyplot library.

Upvotes: 1

Related Questions