如何在 python plotly scattergeo line map 中分离不同的轨迹

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

我在 python 中使用 plotly scattergeo 绘制不同的路线,我的问题是我无法将不同行程之间的线路分开,并且所有线路都连接在一起,就好像它们只是一个。

这张图片中有两次旅行,厄瓜多尔吉尼到里斯本和厄瓜多尔吉尼到开普敦,但即使有两次单独的旅行,也有一条连接线从旅行 1 的终点(到里斯本)到旅行 2 的起点

这是我用来生成绘图的代码:

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()

我的目标是将不同的痕迹分开,而不是全部连接起来。

鉴于 trip1 在索引 7 处结束,您可以按行程拆分 lonlat。完整代码在这里

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()