尝试使用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的例子。有什么我想念的吗?
答案 0 :(得分:1)
我想通了 - 我一再错过的简单语法错误。
在我添加标记的循环中,正确的语法是
folium.Marker(pos, icon).add_to(map)
正如我现在所做的那样,我试图将icon参数添加到地图中,而不是整个标记。
答案 1 :(得分:1)
folium.Marker(location=[lat, lon], icon=folium.Icon(color='blu**e')).add_to(route_map)**
以粗体字检查错误