简体   繁体   English

如何在python plotly scattergeo线图中分离不同的轨迹

[英]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.我正在使用 python 中的 plotly scattergeo 绘制不同的路线,我的问题是我无法将不同行程之间的线分开,并且所有路线都连接在一起,就好像它们只有一个一样。

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

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此图中有两个行程,厄瓜多尔到里斯本和厄瓜多尔到开普敦,但即使有两个单独的行程,从行程 1(到里斯本)的终点到行程 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.我的目标是将不同的痕迹分开,而不是全部连接起来。

Given that trip1 ends at index 7 you can split lon and lat by trip.鉴于 trip1 在索引 7 处结束,您可以按行程拆分lonlat 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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM