添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

在Folium地图上创建一个图例

15 人关注

Folium的文件目前还不完整。 https://folium.readthedocs.io/en/latest/

根据不完整文档的索引,Legends和Layers已经或将要被支持。我花了一些时间在网上寻找例子,但到目前为止还没有找到。如果有人知道如何创建这些东西,或能给我指出一个文档或教程,我将非常感激。

python
folium
sparrow
sparrow
发布于 2016-05-27
5 个回答
Harley
Harley
发布于 2017-12-12
已采纳
0 人赞同

你可以很容易地添加一个图例。

#specify the min and max values of your data
colormap = branca.colormap.linear.YlOrRd_09.scale(0, 8500)
colormap = colormap.to_step(index=[0, 1000, 3000, 5000, 8500])
colormap.caption = 'Incidents of Crime in Victoria (year ending June 2018)'
colormap.add_to(world_map)

你可以在这里看到我的完整例子。

带图例的对开地图

谢谢@用户,这非常有用,我更多的是在寻找一种方法来创建基于离散值的图例,这些离散值由某些颜色表示为标记。
如何增加标题的大小,请帮助我
sparrow
sparrow
发布于 2017-12-12
0 人赞同

Folium现在有了0.15版本,可以轻松添加图片。

from folium.plugins import FloatImage
image_file = 'image.PNG'
FloatImage(image_file, bottom=0, left=86).add_to(mymap)
    
Jas
我也可以从地图上删除图片吗?
Penny Pang
Penny Pang
发布于 2017-12-12
0 人赞同

Try using

feature_group = FeatureGroup(name='Layer1')
feature_group2 = FeatureGroup(name='Layer2')

然后添加到你的地图上

map = folium.Map(zoom_start=6)
# coordinates to locate your marker
COORDINATE = [(333,333)] # example coordinate
COORDINATE2 = [(444,444)]
# add marker to your map
folium.Marker(location=COORDINATE).add_to(feature_group)
folium.Marker(location=COORDINATE2).add_to(feature_group2)
map.add_child(feature_group)
map.add_child(feature_group2)
# turn on layer control
map.add_child(folium.map.LayerControl())
    
谢谢,这不是一个真正的图例,因为它没有把地图上的标记与某一特定事物联系起来,但它仍然是一个有用的提示。
InLaw
InLaw
发布于 2017-12-12
0 人赞同

如果你在Marker或PolyLine的名称参数中添加html,你可以在图层控制中获得一半的文本/标签颜色。

import folium
print( folium.__version__)
import numpy as np
lon_ct = 50
fkt=10
num = 60
m = folium.Map((lon_ct, 6), tiles='stamentoner', zoom_start=6 )
lats = (lon_ct * np.cos(np.linspace(0, 2*np.pi, num))/fkt ) + lon_ct
lons = (lon_ct * np.sin(np.linspace(0, 2*np.pi, num))/fkt ) + 10
colors = np.sin(5 *    np.linspace(0, 2*np.pi, num))
lgd_txt = '<span style="color: {col};">{txt}</span>'
for idx, color in enumerate( ['red', 'blue']):  # color choice is limited
    print(color)
    fg = folium.FeatureGroup(name= lgd_txt.format( txt= color+' egg', col= color))
    pl = folium.features.PolyLine(
            list(zip(lats, lons - idx*fkt)),
            color=color,            
            weight=10, )
    fg.add_child(pl)
    m.add_child( fg)
folium.map.LayerControl('topleft', collapsed= False).add_to(m)

Source: html_legend

如果你知道一点HTML。

item_txt = """<br> &nbsp; {item} &nbsp; <i class="fa fa-map-marker fa-2x" style="color:{col}"></i>"""
html_itms = item_txt.format( item= "mark_1" , col= "red")
legend_html = """
     <div style="
     position: fixed; 
     bottom: 50px; left: 50px; width: 200px; height: 160px; 
     border:2px solid grey; z-index:9999; 
     background-color:white;
     opacity: .85;
     font-size:14px;
     font-weight: bold;
     &nbsp; {title} 
     {itm_txt}
      </div> """.format( title = "Legend html", itm_txt= html_itms)
map.get_root().html.add_child(folium.Element( legend_html ))

Link 基本的

Link 先进的

m.get_root().html
<branca.element.Element at 0x7f5e1ca61250>

https://pypi.org/project/branca/

  • for further manipulations
  • 这很好,但我在文档中找不到这些方法,介意分享一个链接吗?我想知道,是否有可能在以后删除HTML的孩子?
    @RicardoSanchez 这是一个 branca.element.Element
    @InLaw 遗憾的是,在第一个例子中,我只能让地图充满白色。不知道哪里出了问题。
    Dan Kinn
    Dan Kinn
    发布于 2017-12-12
    0 人赞同

    我也遇到了同样的问题,于是用这个快速黑客方法在Folium生成的HTML中添加了一个图例。这不是特别优雅,但它是有效的。因为我只需要几次,所以我手动将图例生成为图片(legend.png),但我想如果你经常这样做,你可以创建一个脚本来自动创建图例。 我在Folium输出地图的HTML文件的适当部分添加了以下组件。

             <style> #background_img {
                position : absolute;
                background:url('legend.png');
                width : 16.9%;
                height: 17.7%;
                right: 20px;
                bottom: 50px;
                z-index: 99;
                background-repeat: no-repeat;
                background-size: contain; 
            </style>