You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OSMnx等距线绘图触发IndexError,求Colab兼容的解决方案

解决方法与替代方案

一、修复当前绘图问题(无需依赖descartes)

由于新版shapely的Polygon外部环格式和descartes不兼容,直接改用matplotlib原生的Polygon类替代PolygonPatch即可,修改后的代码如下:

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

# 绘制路网
fig, ax = ox.plot_graph(G, show=False, close=False, edge_color='#999999', edge_alpha=0.2, node_size=0)
for polygon, fc in zip(isochrone_polys, iso_colors):
    # 提取多边形的坐标点(适配新版shapely格式)
    coords = list(polygon.exterior.coords)
    # 用matplotlib原生Polygon创建绘图补丁
    patch = Polygon(coords, fc=fc, ec='none', alpha=0.6, zorder=-1)
    ax.add_patch(patch)
plt.show()

核心逻辑是直接从shapely多边形中提取坐标列表,绕开descartes的兼容性问题。

二、更简便的替代方案:用OSMnx内置函数绘制等距线

OSMnx的新版本已经内置了适配新版shapely的等距线绘图功能,不需要手动处理补丁,代码更简洁:

# 假设已计算得到isochrone_polys和iso_colors
fig, ax = ox.plot_graph(G, edge_color='#999999', edge_alpha=0.2, node_size=0)
ox.plot_footprints(isochrone_polys, ax=ax, color=iso_colors, alpha=0.6, zorder=-1)
plt.show()

三、降级shapely版本(Colab可操作)

如果一定要沿用原教程的代码,在Colab中可以手动降级shapely到兼容descartes的版本,执行以下命令:

!pip install shapely==1.8.5

执行后重启运行时,再运行原代码即可正常执行。

内容的提问来源于stack exchange,提问作者devara46

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 02:45:07