You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly坐标点间轨迹绘制失败,求助排查shift函数问题

问题排查:Plotly绘制点间轨迹无显示

问题根源

  1. 原循环中直接传入df['long'].shift()和df['long'],这是整个Series而非单个点对,Plotly无法识别该格式渲染单条轨迹
  2. 第一个点(索引0)的shift()结果为NaN,会生成无效的空轨迹

修正方案

调整轨迹绘制逻辑:从第2个点(索引1)开始,每次取当前点与前一个点的经纬度组成坐标对,以此生成单条有效轨迹。

完整修正代码

import pandas as pd
import numpy as np
import plotly.graph_objects as go

def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6371):
    if to_radians:
        lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])

    a = np.sin((lat2-lat1)/2.0)**2 + \
        np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2
    return earth_radius * 2 * np.arcsin(np.sqrt(a))

# 示例数据
data = {'name': {0: 'Veronica Session', 1: 'Lynne Donahoo', 2: 'Debbie Hanley', 3: 'Lisandra Earls', 4: 'Sybil Leef', 5: 'Jessica Mundell', 6: 'Alexandra James', 7: 'Henry Coffell', 8: 'Rose Teich', 9: 'Eric Martinez'}, 'age (years)': {0: 10, 1: 12, 2: 14, 3: 11, 4: 6, 5: 9, 6: 11, 7: 9, 8: 15, 9: 12}, 'home longitude (degrees)': {0: 14.136215105724062, 1: -82.35073741128672, 2: -91.34452348775748, 3: -138.97669945387673, 4: 33.95932301441238, 5: -9.797522458915012, 6: 35.018161767736274, 7: -84.5876956056233, 8: 151.92765611583934, 9: -19.67088886507668}, 'home latitude (degrees)': {0: 11.463797540119938, 1: 44.40537036648498, 2: 14.928904726779818, 3: 68.95146392131102, 4: -1.6783561807047136, 5: 14.45333967686018, 6: -5.605614947366739, 7: 13.073486216442122, 8: -83.03912875968318, 9: 65.57777064215732}, 'lat': {0: 11.463797540119938, 1: 44.40537036648498, 2: 14.928904726779818, 3: 68.95146392131102, 4: -1.6783561807047136, 5: 14.45333967686018, 6: -5.605614947366739, 7: 13.073486216442122, 8: -83.03912875968318, 9: 65.57777064215732}, 'long': {0: 14.136215105724062, 1: -82.35073741128672, 2: -91.34452348775748, 3: -138.97669945387673, 4: 33.95932301441238, 5: -9.797522458915012, 6: 35.018161767736274, 7: -84.5876956056233, 8: 151.92765611583934, 9: -19.67088886507668}, 'dist': {0: float('nan'), 1: 9625.250717024943, 2: 3385.8956180372165, 3: 6859.237699961511, 4: 12515.84742596659, 5: 5140.1303406848665, 6: 5421.465603538021, 7: 13350.896827699213, 8: 11879.820752865564, 9: 18062.08762667251}}
df = pd.DataFrame(data)

df['dist'] = haversine(df['lat'].shift(), df['long'].shift(), df['lat'], df['long'])

fig = go.Figure()

# 绘制静态点
fig.add_trace(go.Scattergeo(
    locationmode = 'ISO-3',
    lon = df['long'],
    lat = df['lat'],
    hoverinfo = 'text',
    mode = 'markers',
    text = df['name'],
    marker = dict(
        size = 2,
        color = 'rgb(255, 0, 0)',
        line = dict(
            width = 3,
            color = 'rgba(68, 68, 68, 0)'
        )
    )))

# 修正后的轨迹绘制逻辑
for i in range(1, len(df)):
    fig.add_trace(
        go.Scattergeo(
            locationmode = 'ISO-3',
            lon = [df['long'].iloc[i-1], df['long'].iloc[i]],
            lat = [df['lat'].iloc[i-1], df['lat'].iloc[i]],
            mode = 'lines',
            line = dict(width = 1, color = 'red'),
        )
    )

fig.update_layout(
    title_text = '点间轨迹可视化',
    showlegend = False,
    geo = dict(
        scope = 'world',
        projection_type = 'azimuthal equal area',
        showland = True,
        landcolor = 'rgb(243, 243, 243)',
        countrycolor = 'rgb(204, 204, 204)',
    ),
)

fig.show()

关键修改说明

  • 循环起始索引改为1,跳过无前置点的第一个记录
  • 用iloc[i-1]和iloc[i]获取单个坐标值,组成长度为2的列表,对应单条轨迹的起点和终点

内容的提问来源于stack exchange,提问作者Milo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 05:40:41