如何在python绘图分散的地理线图中分离不同的轨迹

2024-06-24 12:33:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python中的plotly scattergeo绘制不同的路线,我的问题是我无法将不同路线之间的线路分开,所有线路都连接在一起,就好像它们只是一条线路一样

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)

在这张图片中有两次行程,从厄瓜多尔吉尼到里斯本和从厄瓜多尔吉尼到开普敦,但即使有两次单独的行程,也有一条从行程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()

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


Tags: 代码importgo绘图fig绘制图片plotly
1条回答
网友
1楼 · 发布于 2024-06-24 12:33:22

假设trip1在索引7处结束,您可以通过trip分割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()

enter image description here

相关问题 更多 >