Ignacio Valenzuela
Ignacio Valenzuela

Reputation: 137

How to separate different traces in a python plotly scattergeo line map

I'm plotting different routes using plotly scattergeo in python and my issue is that i'm not able to separate the lines between different trips, and all are joined as if they're only one.

There are two trips in this image, Ecuatorial Guiney to Lisbon and  Ecuatorial Guiney to Cape Town, but even with two separate trips there's a connecting line from the end of trip 1 (to Lisbon) to the start of trip 2 (to Guiney)

There are two trips in this image, Ecuatorial Guiney to Lisbon and Ecuatorial Guiney to Cape Town, but even with two separate trips there's a connecting line from the end of trip 1 (to Lisbon) to the start of trip 2

This is the code I'm using to generate the plot:

import plotly.graph_objects as go


lat = [1.769395, 3.909938, 4.416706, 4.402889, 4.470340,
       9.905343,14.541283, 38.611303, 1.769395,2.958316,
       -6.161784, -12.832035, -22.959316, -34.089891]
lon = [9.687394, 9.012994, 7.696527, 5.590180, -4.445836,
       -15.484433, -23.936471, -9.516133, 9.687394, 12.089027,
       -4.623525, 12.121931, 10.773240, 17.804489]

fig = go.Figure(go.Scattermapbox(
        mode="markers+lines",
        lon=lon,
        lat=lat,
        marker={'size': 10}))

fig.update_layout(
        margin={'l': 0, 't': 0, 'b': 0, 'r': 0},
        mapbox={
            'center': {'lon': 10, 'lat': 10},
            'style': "stamen-terrain",
            'center': {'lon': -20, 'lat': -20},
            'zoom': 1})
#To be able to see the plot while using pycharm
fig.write_image('C:/Users/user/Desktop/test.png')
fig.show()

My aim would be to have different traces separated, not all joined up.

Upvotes: 1

Views: 2325

Answers (1)

rpanai
rpanai

Reputation: 13437

Given that trip1 ends at index 7 you can split lon and lat by trip. Here the full code

import plotly.graph_objects as go

lat = [1.769395, 3.909938, 4.416706, 4.402889, 4.470340,
       9.905343,14.541283, 38.611303, 1.769395,2.958316,
       -6.161784, -12.832035, -22.959316, -34.089891]
lon = [9.687394, 9.012994, 7.696527, 5.590180, -4.445836,
       -15.484433, -23.936471, -9.516133, 9.687394, 12.089027,
       -4.623525, 12.121931, 10.773240, 17.804489]

lon_trip1 = lon[:8]
lat_trip1 = lat[:8]
lon_trip2 = lon[8:]
lat_trip2 = lat[8:]

fig = go.Figure()
fig.add_trace(go.Scattermapbox(
        mode="markers+lines",
        lon=lon_trip1,
        lat=lat_trip1,
        name="trip1",
        marker={'size': 10}))
fig.add_trace(go.Scattermapbox(
        mode="markers+lines",
        lon=lon_trip2,
        lat=lat_trip2,
        name="trip2",
        marker={'size': 10}))

fig.update_layout(
        margin={'l': 0, 't': 0, 'b': 0, 'r': 0},
        mapbox={
            'center': {'lon': 10, 'lat': 10},
            'style': "stamen-terrain",
            'center': {'lon': -20, 'lat': -20},
            'zoom': 1})
#To be able to see the plot while using pycharm
# fig.write_image('C:/Users/user/Desktop/test.png')
fig.show()

enter image description here

Upvotes: 3

Related Questions