这里使用到matlibplot包和cartopy包。
安装matlibplot:pip install matlibplot
安装cartopy:
conda install -c conda-forge cartopy
import os
import sys
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
GPS_data = np.loadtxt('文件路径', dtype="str", delimiter=None, skiprows=1, usecols=None, unpack=False)
ma=np.array(GPS_data[:,2:4], dtype=float, copy=True, order=None, subok=False, ndmin=0)
gps_data = ma
fig = plt.figure(figsize=(30, 30))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.Mercator())
lonabs=max(ma[:,0])-min(ma[:,0])
latabs=max(ma[:,1])- min(ma[:,1])
# 设置地图范围
ax.set_extent([(min(ma[:,0])-lonabs*0.1),(ma[:,0].max()+lonabs*0.1),( ma[:,1].min()-latabs*0.1),(ma[:,1].max()+latabs*0.1)])
ax.add_feature(cfeature.BORDERS, edgecolor='black', linewidth=0.5) # 添加国界线
ax.add_feature(cfeature.COASTLINE, edgecolor='black', linewidth=0.5) # 添加海岸线
ax.add_feature(cfeature.LAND, facecolor='coral') # 添加陆地颜色
ax.add_feature(cfeature.OCEAN, facecolor='aqua') # 添加海洋颜色
# 转换并绘制GPS轨迹
#设置横纵坐标
# 设置坐标轴标签
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.plot(gps_data[:, 0], gps_data[:, 1], transform=ccrs.Geodetic(), marker='*', linestyle='-', color='blue', linewidth=1,label='Real ')
ax.plot(gps_data[0, 0], gps_data[0, 1], transform=ccrs.Geodetic(), marker='o', linestyle='-', color='red', linewidth=10)
# ax.scatter(gps_data[0, 0],gps_data[0, 1],s=10,color='red')
plt.legend(fontsize=(32))#图标大小为latabs*100,通常设置16,32
plt.savefig('保存文件的路径'.png')
print("\r",file_path,end='')
except Exception as e:
print('error fail file:',file_path)
print(e)