如何使用 matplotlib 在地图上绘制线段?

问题描述

我正在尝试根据变量的值在地图(最好是正交投影)上绘制不同颜色的线段。我有一张船的连续纬度/经度位置列表,以及这些连续位置之间测量的距离。

所以,我试图在 pos1(lat1/lon1) 和 pos2(lat2/lon2)、pos2(lat2/lon2) 和 pos3(lat3/lon3) 之间绘制线段,直到 [n-1]位置,颜色取决于距离值。当我运行此代码时,我发现奇怪的线段位置错误

plot

我在 SO 上发现了类似的问题,但没有在地理轴上绘图。我究竟做错了什么?我尝试了很多东西,将经度更改为 0-360,更改 PlateCarree 项目的中心经度。请帮忙。谢谢

数据文件ship tracks

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.ticker as mticker
from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


shp_trk=pd.read_csv('/Users/prt/ship_tracks_distances.csv')

y = np.asarray(shp_trk['Plat'])
x = np.asarray(shp_trk['Plong'])
dis = np.asarray(shp_trk['dis'])

#segments
points = np.array([x,y]).T.reshape(-1,1,2)
segments = np.concatenate([points[:-1],points[1:]],axis=1)

fig = plt.figure(figsize=[20,10])
ax = fig.add_subplot(1,projection=ccrs.PlateCarree(central_longitude=180))
#ax.set_extent([lonmin,lonmax,latmin,latmax],crs=ccrs.PlateCarree())
#ax = fig.add_subplot(1,projection=ccrs.Orthographic(central_latitude=0,central_longitude=190))

ax.stock_img()
ax.coastlines()

plt.title('Salt Lake City\n'
          'MS',fontdict={'fontsize':16})


norm = plt.normalize(dis.min(),dis.max())
lc = LineCollection(segments,antialiaseds=(1,),cmap='viridis',norm=norm)
# Set the values used for colormapping
lc.set_array(dis)
lc.set_linewidth(2)
#lc.set_edgecolors('red')
line = ax.add_collection(lc)
fig.colorbar(line,ax=ax)


#Pearl Harbour
plt.plot(-157.9491,21.3618,'yo',markersize=7,transform=ccrs.Geodetic())
plt.text(-155,18,'Pearl Harbour',fontdict={'fontsize':16},transform=ccrs.Geodetic())
#San Diego
plt.plot(-117.1625,32.715,transform=ccrs.Geodetic())
plt.text(-117,33,'San Diego',transform=ccrs.Geodetic())
#Brisbane Harbour
plt.plot(153.2469,-27.2491,transform=ccrs.Geodetic())
plt.text(153,-27.2,'Brisbane',transform=ccrs.Geodetic())


plt.show()

但是,当我在笛卡尔网格上绘制它时,它工作正常。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...