1

I am working on creating a map that connects coordinates using a line with scattermapbox. Using the example here, I have been able to connect each coordinate to one another.

However, I just want to connect coordinates that have the value of "Destination" under the column "Key" to the one coordinate in my dataset that has the value of "Origin" for that same column.

Here is my data:

import pandas as pd

lat_dest = [21.028321, 10.426675, 10.776390,-35.297591,21.028321]
lon_dest = [105.854022,-75.544342,106.701139,149.101268,105.854022]
locations = ['Hanoi, Vietnam','Cartagena, Colombia','Ho Chi Minh City, Vietnam','Canberra, Australia','Hanoi, Vietnam']
dest_key = ['Destination','Destination','Destination','Destination','Origin']

test_df = pd.DataFrame({
  'lat': lat_dest,
  'lon': lon_dest,
  'name': locations,
  'key':dest_key
})
 

Here is the map:

import plotly.express as px
import plotly.graph_objects as go

fig = px.scatter_mapbox(
    test_df, 
    lat="lat", lon="lon", 
    color="key",
    zoom=2,
    center = {"lat": orig_lat, "lon": orig_lon}
)

fig.update_layout(
    mapbox_style="carto-positron",
    margin={"r":0,"t":0,"l":0,"b":0},
    showlegend=False
)

fig.add_trace(go.Scattermapbox(
    mode = "lines",
    lon = new_df['lon_dest'],
    lat = new_df['lat_dest']
))
user2813606
  • 797
  • 2
  • 13
  • 37
  • The current output would be a polygon with lines connecting the four locations. How would you like to do that? Please explain in detail. If you need a line from the origin to each point, you would need to draw a line with data such as: `lat=[latitude_origin, latitude_pointA];lon=[longitude_origin, longitude_pointA]`. – r-beginners Aug 03 '23 at 04:30
  • I added : fig.add_trace(go.Scattermapbox( mode = "lines", lon = [test_df['lon'],test_df['lon'].iloc[-1]], lat = [test_df['lat'],test_df['lat'].iloc[-1]] )) It didn't result in an error, but did erase the line. – user2813606 Aug 03 '23 at 12:28
  • I may not have communicated it well. The latitude of the start point, the latitude of the end point, the longitude of the start point, and the longitude of the end point are two points. – r-beginners Aug 03 '23 at 12:47
  • `fig.add_trace(go.Scattermapbox(mode="lines", lon=[lon_dest[0],lon_dest[1]], lat=[lat_dest[0],lat_dest[1]]));fig.add_trace(go.Scattermapbox(mode="lines", lon=[lon_dest[0],lon_dest[2]], lat=[lat_dest[0],lat_dest[2]])` – r-beginners Aug 03 '23 at 12:54
  • Is there a way to write this so I don't have to write a separate trace for each destination coordinate? – user2813606 Aug 03 '23 at 12:59
  • `for lon,lat in zip(lon_dest[1:-1],lat_dest[1:-1]): fig.add_trace(go.Scattermapbox(mode="lines", lon=[lon_dest[-1],lon], lat=[lat_dest[-1],lat]))` – r-beginners Aug 03 '23 at 13:11
  • This works! Thank you! Final question: how can I get all the colors of the lines to be the same? – user2813606 Aug 03 '23 at 15:20
  • Try this: `fig.add_trace(go.Scattermapbox(mode="lines", lon=[lon_dest[-1],lon], lat=[lat_dest[-1],lat], line_color='blue'))` – r-beginners Aug 04 '23 at 01:39
  • It works! If you put this as an answer, I can award you points! – user2813606 Aug 04 '23 at 19:43

1 Answers1

1

To draw a line from one point to another, you must specify the latitude and longitude of the origin and the latitude and longitude of each end point. So, if the data is in your list format, it can be simplified by a loop process. Finally, thank you for the opportunity to respond.

import plotly.express as px
import plotly.graph_objects as go

fig = px.scatter_mapbox(
    test_df, 
    lat="lat", lon="lon", 
    color="key",
    zoom=2,
    center = {"lat": test_df['lat'].mean(), "lon": test_df['lon'].mean()}
)

fig.update_layout(
    mapbox_style="carto-positron",
    margin={"r":0,"t":0,"l":0,"b":0},
    showlegend=False
)

for lon,lat in zip(lon_dest[1:-1],lat_dest[1:-1]):
    fig.add_trace(go.Scattermapbox(mode="lines",
                                   lon=[lon_dest[-1],lon],
                                   lat=[lat_dest[-1],lat],
                                   line_color='blue')) 

fig.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32