Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot a route in a map

I'm using python and I need to plot a route in a map. I have a dataframe that looks like this:

  latitude  longitude
  41.393095  -8.703483
  41.393095  -8.703483
  41.393095  -8.703483
  41.392483  -8.703088
  40.942170  -8.540572
  40.942188  -8.540567
  41.187624  -8.568143
  41.321009  -8.711874
  41.345618  -8.547567

The order of the dataframe represents the order of the route and I would like to plot it based on latitude and longitude. But i only find ways to plot it based on osm node IDs.

Does anyone know a way of plotting this route with the exact geographic coordinates? Thanks!

like image 514
Beatriz Santos Avatar asked Nov 01 '25 10:11

Beatriz Santos


1 Answers

Using This tutorial, I managed to plot the points on a map:

Image of map with path

# Create a bounding box to determine the size of the required map
BBox = (df.longitude.min()-0.1,   df.longitude.max()+0.1,
        df.latitude.min()-0.1, df.latitude.max()+0.1)

# Downloaded using this tutorial: https://medium.com/@abuqassim115/thanks-for-your-response-frank-fb869824ede2
map_img = plt.imread('map.png')

# Plotting the points on the graph
fig, ax = plt.subplots(figsize=(8, 7))
ax.plot(df.longitude, df.latitude, 'xb-')

# Setting limits for the plot
ax.set_xlim(BBox[0], BBox[1])
ax.set_ylim(BBox[2], BBox[3])

# Showing the image behind the points
ax.imshow(map_img, zorder=0, extent=BBox, aspect='equal')

plt.show()
like image 137
Adid Avatar answered Nov 04 '25 00:11

Adid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!