【问题标题】:How to add legend/gradient in Folium Heat Map?如何在叶热图中添加图例/渐变?
【发布时间】:2018-04-20 04:43:41
【问题描述】:

我正在使用 Folium 创建热图。我的数据包含 3 列,一列是类别、纬度和经度。经纬度点分为 A、B 和 C 3 类。 我可以使用 folium 绘制热图,但我需要添加显示点之间颜色差异的图例。我需要根据类别将点标记为 3 种不同的颜色。

我附上示例代码供您参考。感谢任何帮助。

提前致谢!

from folium import plugins
from folium.plugins import HeatMap
from folium.plugins import MarkerCluster
import pandas as pd

map = folium.Map(location=[lat, long],zoom_start =12) 
data = pd.read_csv(filename)
# List comprehension to make out list of lists
heat_data = [[row['LAT'],row['LONG'],] for index, row in data.iterrows()]
# Plot it on the map
HeatMap(heat_data).add_to(map)
# Display the map
map
map.save('C:\Temp\map2.html')

【问题讨论】:

    标签: python gradient legend heatmap folium


    【解决方案1】:

    以下是解决方案,它基于@Alexei Novikov 的回答 下面的代码比较完整

    import branca.colormap
    from collections import defaultdict
    import folium
    import webbrowser
    from folium.plugins import HeatMap 
    
    map_osm = folium.Map(llocation=[35,110],zoom_start=1)
    
    steps=20
    colormap = branca.colormap.linear.YlOrRd_09.scale(0, 1).to_step(steps)
    gradient_map=defaultdict(dict)
    for i in range(steps):
        gradient_map[1/steps*i] = colormap.rgb_hex_str(1/steps*i)
    colormap.add_to(map_osm) #add color bar at the top of the map
    
    HeatMap(data1,gradient = gradient_map).add_to(map_osm) # Add heat map to the previously created map
    
    file_path = r"C:\\test.html"
    map_osm.save(file_path) # Save as html file
    webbrowser.open(file_path) # Default browser open
    

    【讨论】:

      【解决方案2】:

      您必须使用颜色图值创建字典

      steps = 20
      color_map=cm.linear.PuBu.scale(0,1).to_step(steps)
      
      gradient_map=defaultdict(dict)
      for i in range(steps):
          gradient_map[1/steps*i] = color_map.rgb_hex_str(1/steps*i)
      

      然后将其用作 HeatMap 的渐变。

      HeatMap(heat_data, gradient = gradient_map)
      

      【讨论】:

      • 怎么确定defaultdict(dict)
      • 在这种情况下cm 等于多少?
      • 刚刚想通了:import branca.colormap as cm
      猜你喜欢
      • 2017-07-22
      • 2021-07-27
      • 2019-12-04
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 2019-01-27
      • 2020-10-13
      • 1970-01-01
      相关资源
      最近更新 更多