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

如何使用Folium在Jupyter中批量显示多张地图?

问题:Jupyter中批量渲染多张Folium地图无显示

我有一些GPS数据需要在地图上绘制,已经成功用Folium在Jupyter中单次绘制一组数据:

mapPoints = [ [lat1, lng1], [lat2, lng2], ... ]
m = folium.Map(location=location, zoom_start=15)
folium.PolyLine(mapPoints).add_to(m)
m

这段代码能正常显示预期的地图,但尝试生成多张地图时,没有任何内容显示:

maps = []
for item in clustersOfPoints:
  mapPoints = getListOfLatLngs(item)
  m = folium.Map(location=location, zoom_start=15)
  folium.PolyLine(mapPoints).add_to(m)
  maps.append(m)
  m
...
for map in maps:
  map

已验证循环内的mapPoints列表正确,且maps变量包含folium.folium.Map对象,请问如何实现多张地图的渲染显示?


解决方案

Jupyter Notebook仅会自动渲染代码块最后一行的对象,循环中直接写对象名的方式无法触发渲染逻辑。要批量显示多张Folium地图,需借助IPython的display模块:

  1. 先导入所需模块
from IPython.display import display
  1. 修改代码,用display()方法逐个渲染地图:
maps = []
for item in clustersOfPoints:
  mapPoints = getListOfLatLngs(item)
  m = folium.Map(location=location, zoom_start=15)
  folium.PolyLine(mapPoints).add_to(m)
  maps.append(m)
  # 循环内实时显示当前生成的地图
  display(m)

# 若需后续再批量显示,可遍历列表调用display
# for map_obj in maps:
#   display(map_obj)

注意:避免使用map作为变量名,它是Python内置函数,容易引发命名冲突,建议改用map_obj这类名称。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 02:10:03