如何用Plotly绘制纬度线?为何同纬度线段显示异常?
问题原因及解决方案
问题根源
你遇到的问题是因为Plotly的scattergeo组件默认使用**大圆路径(Great Circle)**绘制线段。大圆路径是球面上两点间的最短路径,当跨经度范围较大时,这条路径会明显偏离固定纬度的纬线,导致视觉上两条线段不在同一纬度上。
解决方案
要让线段沿固定纬度的纬线(恒向线/Rhumb Line)绘制,只需在每个scattergeo的配置中添加line参数,并将shape设置为'rhumb'即可。
修正后的代码如下:
from plotly.offline import iplot # First latitude line data_dict3 = {'type': 'scattergeo', 'lat': [48,48], 'lon': [-130,-60], 'marker': {'size': 5, 'color': 'rgb(250, 250, 10)' }, 'mode': 'lines', 'line': {'shape': 'rhumb'} # 添加此行,强制沿恒向线绘制 } # Second latitude line data_dict4 = {'type': 'scattergeo', 'lat': [48,48], 'lon': [-170,-100], 'marker': {'size': 5, 'color': 'rgb(10, 160, 0)' }, 'mode': 'lines', 'line': {'shape': 'rhumb'} # 添加此行,强制沿恒向线绘制 } # Map parameters geo_dict = {'showframe': True, 'framewidth': 25, 'scope': 'north america', 'showland': True, 'landcolor': "rgb(212, 212, 212)", 'showocean': True, 'oceancolor': "rgb(180, 180, 225)", 'showlakes': True, 'lakecolor': "rgb(180, 180, 225)", 'subunitcolor': "rgb(0,0,0)", 'resolution': 50} # Layout Dict layout_dict = {'geo': geo_dict} # Dicts defining figure fig = {'data':[data_dict3, data_dict4], 'layout':layout_dict} iplot(fig)
说明
'line': {'shape': 'rhumb'}会强制Plotly使用恒向线绘制线段,这种路径的纬度保持恒定,完全符合你预期的纬线效果。- 大圆路径适合展示航空、航海的最短航线,而恒向线则适用于需要保持固定纬度/经度的场景。
内容的提问来源于stack exchange,提问作者user1245262
相关产品推荐
相关产品推荐

