使用Cartopy运行代码报错:'GeoAxesSubplot'无'_autoscaleXon'属性求解决
解决Cartopy的AttributeError: 'GeoAxesSubplot' object has no attribute '_autoscaleXon'错误
以下是几种可行的解决方法:
确认并安装兼容的Cartopy与Matplotlib版本
虽然你已降级Matplotlib到3.5,但需确保Cartopy版本和该Matplotlib版本完全兼容。例如Cartopy 0.21.x系列与Matplotlib 3.4-3.6兼容。先运行以下命令查看当前版本:pip show cartopy pip show matplotlib若版本不匹配,执行指定版本安装:
pip install cartopy==0.21.0 matplotlib==3.5.3替换内部属性调用为公开API
错误源于代码直接访问了Matplotlib的内部属性_autoscaleXon,而Cartopy的GeoAxesSubplot并未暴露该属性。将代码中涉及_autoscaleXon的部分替换为Matplotlib公开方法:- 开启自动缩放:
ax.autoscale(enable=True, axis='x') - 手动设置轴范围:
ax.set_xlim(your_min_x, your_max_x)、ax.set_ylim(your_min_y, your_max_y)
- 开启自动缩放:
清理依赖缓存后重新安装
残留的旧版本缓存文件可能导致版本冲突,按以下步骤操作:# 卸载现有包 pip uninstall -y cartopy matplotlib # 清理pip缓存 pip cache purge # 重新安装兼容版本 pip install cartopy==0.21.0 matplotlib==3.5.3检查并更新过时的Cartopy用法
若代码中使用了旧版Cartopy的绘图逻辑(如某些已废弃的轴设置方法),可能触发内部属性调用。例如调整ax.set_extent()的参数格式以符合当前Cartopy版本要求,或直接用set_xlim/set_ylim替代。
内容的提问来源于stack exchange,提问作者Giorgos
相关产品推荐
相关产品推荐

