在Cartopy中,替代Basemap的npts函数的方法是使用cartopy.crs.CRS
对象的transform_points
方法。transform_points
方法可以将经纬度坐标转换为指定投影的坐标。
下面是一个使用transform_points
方法的示例代码:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
# 创建一个CRS对象,例如Mercator投影
crs = ccrs.Mercator()
# 定义经纬度坐标
lons = np.array([-80, -70, -60])
lats = np.array([30, 40, 50])
# 将经纬度坐标转换为Mercator投影的坐标
x, y, _ = crs.transform_points(ccrs.PlateCarree(), lons, lats).T
# 打印转换后的坐标
for i in range(len(x)):
print(f"({lons[i]}, {lats[i]}) -> ({x[i]}, {y[i]})")
# 绘制转换后的坐标
plt.plot(x, y, marker='o', linestyle='None', transform=crs)
plt.show()
在上面的代码中,首先创建了一个ccrs.Mercator()
对象作为目标投影(替代Basemap中的npts函数的第一个参数),然后使用transform_points
方法将经纬度坐标转换为Mercator投影的坐标。最后,使用Matplotlib绘制转换后的坐标。
运行这段代码,你会发现经纬度坐标被成功转换为Mercator投影的坐标,并绘制出来。
下一篇:Basemap只显示南极洲的一半