问题描述
我想使用Matplotlib(v3.1.3
)和cartopy(v0.17.0
)绘制一组UTM坐标,然后手动设置轴的范围。通常,我可以使用axis.set_extent((left,right,bottom,top))
进行此操作,但是当我尝试使用UTM坐标进行此操作时,会收到一条错误消息,声称我的坐标超出了允许的范围。当我从字面上复制并插入当前轴范围(使用axis.get_extent()
)时,也会发生这种情况。
请参见以下最小示例:
import cartopy
import cartopy.crs as ccrs
import numpy as np
import matplotlib.pyplot as plt
# Some random UTM coordinates
UTM = np.array([
[328224.965,4407328.289],[328290.249,4407612.599],[328674.439,4408309.066],[327977.178,4407603.320],[328542.037,4408510.581]
]).T
# Split into east and north components
east,north = UTM
# Create a canvas with UTM projection
fig = plt.figure()
ax = fig.add_subplot(111,projection=ccrs.UTM(zone="11S"))
# Plot coordinates
ax.scatter(east,north)
# Get the extent of the axis
extent = ax.get_extent()
# Attempt to set the axis extent
ax.set_extent(extent)
plt.show()
这引发了以下异常:
ValueError: Failed to determine the required bounds in projection coordinates. Check that the values provided are within the valid range (x_limits=[-250000.0,1250000.0],y_limits=[-10000000.0,25000000.0]).
解决方法
代码行:
ax.set_extent(extent)
具有选项crs=None
,它转换为将ccrs.PlateCarree()
作为默认值。这意味着代码中的extent
中应包含经度和纬度的值。
为正确起见,您必须指定正确的crs:-
ax.set_extent(extent,crs=ccrs.UTM(zone="11S"))