尝试使用 Folium 从数据框中的位置生成交互式地图pandas
。但是,当我尝试将地图另存为 HTML 文件时,我收到一个断言错误:“如果它不在图中,则无法渲染此元素”。
我能找到的唯一相关信息是一个较旧的论坛帖子,该帖子已关闭,没有足够的细节让我了解如何修复它:
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 的示例。有什么我想念的吗?