2026, Jan 09 11:00

How to Fix Missing OSMnx Routes: Pass X=Longitude and Y=Latitude to nearest_nodes

OSMnx route line not appearing? The fix is coordinate order: use X=longitude, Y=latitude in nearest_nodes. See why plotting fails and how to render routes.

When you compute a driving route with OSMnx, the map renders but the red line never appears. The graph is there, the window opens, yet the route seems to be missing. In most cases like this, the issue isn’t in pathfinding at all—it’s the order of coordinates passed to the nearest-nodes lookup.

Repro: route not drawn

The snippet below builds a drivable street network for New York, looks up the nearest graph nodes to a start and end position, computes the shortest path, and plots the result. The map appears, but the route line does not show up as expected.

import osmnx as ox
net = ox.graph_from_place("New York, New York", network_type="drive")
# Latitude and longitude for origin and destination
lat_a, lon_a = 40.73765090370579, -73.97428265598312
lat_b, lon_b = 40.72568284052202, -74.01119033844985
# Nearest nodes lookup (incorrect coordinate order)
node_a = ox.nearest_nodes(net, X=lat_a, Y=lon_a)
node_b = ox.nearest_nodes(net, X=lat_b, Y=lon_b)
route_ids = ox.shortest_path(net, node_a, node_b, weight="length")
fig, ax = ox.plot_graph_route(net, route_ids, route_color="r", route_linewidth=6, node_size=0)

What actually goes wrong

OSMnx’s nearest-nodes function expects coordinates in the order X=longitude and Y=latitude. Swapping them—passing latitude as X and longitude as Y—means the lookup targets the wrong places on the globe. The path you compute is then based on unintended nodes, so the plotted "route" won’t show up where you expect. This coordinate order is specified in the function’s documentation.

Fix: pass X=longitude and Y=latitude

Keep everything else the same and swap the arguments in the nearest-nodes calls so X gets longitude and Y gets latitude. The plotting call will then render the red route over the map as intended.

import osmnx as ox
net = ox.graph_from_place("New York, New York", network_type="drive")
# Latitude and longitude for origin and destination
lat_a, lon_a = 40.73765090370579, -73.97428265598312
lat_b, lon_b = 40.72568284052202, -74.01119033844985
# Nearest nodes lookup (correct coordinate order)
node_a = ox.nearest_nodes(net, X=lon_a, Y=lat_a)
node_b = ox.nearest_nodes(net, X=lon_b, Y=lat_b)
route_ids = ox.shortest_path(net, node_a, node_b, weight="length")
fig, ax = ox.plot_graph_route(net, route_ids, route_color="r", route_linewidth=6, node_size=0)

Why this detail matters

Geospatial tooling often mixes conventions: some APIs expect latitude–longitude, others longitude–latitude. OSMnx’s nearest-nodes lookup is explicit about using X for longitude and Y for latitude. Getting this right ensures you compute a path between the intended locations and that your visualization aligns with the map background. It also prevents downstream logic from operating on the wrong portion of the graph when you later inspect the route or integrate with a UI framework.

Takeaways

If a route fails to appear but the graph renders, verify coordinate order in every call that accepts X and Y. With OSMnx, pass longitude to X and latitude to Y. Once that’s in place, pathfinding and plotting behave as expected, and you can move on to interface work and any route analysis you plan to build on top.