Folium 中的断言错误 - "cannot render this Element if it's not in a Figure"

Assertion error in Folium - "cannot render this Element if it's not in a Figure"

尝试使用 Folium 从 pandas 数据框中的位置生成交互式地图。但是,当我尝试将地图另存为 HTML 文件时,出现断言错误:"You cannot render this Element if it's not in a Figure".

我能找到的唯一相关信息是一个较旧的论坛 post,该论坛已关闭,但没有足够的详细信息让我了解如何修复它:

https://github.com/python-visualization/folium/issues/495

我的代码:

data = pd.read_csv(in_file)

route_map = folium.Map(location=[data['"PosLat"'].mean(), data['"PosLon"'].mean()],
                       zoom_start=10, tiles='OpenStreetMap')

for lat, lon, date in zip(data['"PosLat"'], data['"PosLon"'], data['"Date"_"Time"']):
    folium.Marker(location=[lat, lon],
                  icon=folium.Icon(color='blue').add_to(route_map))

out_file = input('Enter file name: ')
if '.html' not in out_file:
    out_file += '.html'

route_map.save(out_file)

错误:

Traceback (most recent call last):
  File "interactive_map_gen.py", line 21, in <module>
    route_map.save(out_file)
  File "C:\Program Files\Python36\lib\site-packages\branca\element.py", line 157, in save
    html = root.render(**kwargs)
  File "C:\Program Files\Python36\lib\site-packages\branca\element.py", line 301, in render
    child.render(**kwargs)
  File "C:\Program Files\Python36\lib\site-packages\folium\map.py", line 299, in render
    super(LegacyMap, self).render(**kwargs)
  File "C:\Program Files\Python36\lib\site-packages\branca\element.py", line 617, in render
    element.render(**kwargs)
  File "C:\Program Files\Python36\lib\site-packages\branca\element.py", line 598, in render
    assert isinstance(figure, Figure), ("You cannot render this Element "
AssertionError: You cannot render this Element if it's not in a Figure.

上述论坛帖子中唯一建议的解决方法是从 folium 而不是 branca 导入颜色图,但我还没有找到任何关于如何做到这一点的信息。我试过重新安装 folium,我试过将输出文件名设置为固定字符串。我不知所措。一切都遵循 https://pypi.python.org/pypi/folium 的 folium 0.3.0 示例。有什么我想念的吗?

我弄明白了 - 我反复错过的简单语法错误。

在我添加标记的循环中,正确的语法是

folium.Marker(pos, icon).add_to(map)

正如我现在所做的那样,我正在尝试将图标参数添加到地图,而不是整个标记。

folium.Marker(location=[lat, lon], icon=folium.Icon(color='blu**e')).add_to(route_map)**

用粗体字检查你的错误