Dil*_*eep 3 python gradient legend heatmap folium
我正在使用 Folium 创建一个热图。我的数据包含 3 列,其中一个是类别、纬度和经度。经纬度点分为 3 类,如 A、B 和 C。我可以使用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')
Run Code Online (Sandbox Code Playgroud)
以下是解决方案,它基于@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
Run Code Online (Sandbox Code Playgroud)
小智 5
您必须创建一个包含颜色图值的字典
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)
Run Code Online (Sandbox Code Playgroud)
然后将其用作热图的渐变。
HeatMap(heat_data, gradient = gradient_map)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6029 次 |
| 最近记录: |