如何基于coverage字段值为LineString GeoDataFrame实现渐变色彩地图可视化
我来帮你搞定这个LineString按coverage值渐变颜色的可视化需求~结合你已经在使用的folium库,这里有两个实用的方案,先给你最贴合现有代码的交互式方案:
方案一:基于Folium的交互式渐变可视化
首先得写一个颜色映射函数,根据coverage的数值返回对应的渐变颜色:
- 当
coverage在0到0.5之间时:从深红色(#8B0000)线性过渡到浅红色(#FFCCCC) - 当
coverage在0.5到1之间时:从浅蓝色(#CCCCFF)线性过渡到深蓝色(#00008B)
然后通过folium.GeoJson的style_function参数,动态给每个LineString设置对应颜色。直接把下面的代码整合到你的现有逻辑里就行:
import folium from shapely.geometry import LineString import pandas as pd from geopandas import GeoDataFrame from folium.plugins import LegendIcon # ---------------------- 你的已有代码(保留即可) ---------------------- # 假设你已经生成了polyline_gdf和带coverage字段的gdf_line # ... 你的数据准备代码 ... # ---------------------- 新增:颜色映射函数 ---------------------- def get_gradient_color(coverage): if coverage <= 0.5: # 红系渐变:0对应深红,0.5对应浅红 ratio = coverage / 0.5 # 计算RGB分量的线性插值 r = int(139 + (255 - 139) * ratio) g = int(0 + (204 - 0) * ratio) b = int(0 + (204 - 0) * ratio) else: # 蓝系渐变:0.5对应浅蓝,1对应深蓝 ratio = (coverage - 0.5) / 0.5 r = int(204 + (0 - 204) * ratio) g = int(204 + (0 - 204) * ratio) b = int(255 + (139 - 255) * ratio) # 转成十六进制颜色格式 return f'#{r:02X}{g:02X}{b:02X}' # ---------------------- 绘制地图(整合你的现有代码) ---------------------- # 初始化地图(用你原来的配置就行) f = folium.Figure(height=400) m = folium.Map([52.303333, 8.02], zoom_start=15, tiles='Cartodb dark_matter') m.add_to(f) # 添加GPS点(你的原有函数,直接复用) def add_markers(mapobj, gdf): coords = [] for i, row in gdf.iterrows(): coords.append([row.geometry.y, row.geometry.x]) for coord in coords: folium.CircleMarker( location=coord, radius=2.5, fill=True, fill_color='#F50057', fill_opacity=0.75, color='whitesmoke', weight=0.5 ).add_to(mapobj) return mapobj add_markers(m, polyline_gdf) # 添加渐变LineString(核心部分) folium.GeoJson( gdf_line, style_function=lambda feature: { 'color': get_gradient_color(feature['properties']['coverage']), 'weight': 4, # 可以根据需要调整线的粗细 'opacity': 0.8 # 调整线的透明度,避免太突兀 } ).add_to(m) # 可选:添加颜色图例,让读者清楚颜色对应的coverage范围 legend = folium.map.Legend() legend.add_child(LegendIcon(icon='line', color='#8B0000', legend='Coverage = 0')) legend.add_child(LegendIcon(icon='line', color='#FFCCCC', legend='Coverage = 0.5')) legend.add_child(LegendIcon(icon='line', color='#CCCCFF', legend='Coverage = 0.5')) legend.add_child(LegendIcon(icon='line', color='#00008B', legend='Coverage = 1')) legend.add_to(m) # 显示地图 m
方案二:基于Geopandas + Matplotlib的静态可视化
如果你需要生成静态图片用于报告或文档,也可以用Geopandas结合Matplotlib来实现:
import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap # 创建自定义的渐变配色方案 colors = ['#8B0000', '#FFCCCC', '#CCCCFF', '#00008B'] coverage_cmap = LinearSegmentedColormap.from_list('coverage_grad', colors, N=100) # 绘制图形 fig, ax = plt.subplots(figsize=(10, 8)) # 绘制渐变LineString gdf_line.plot( ax=ax, column='coverage', cmap=coverage_cmap, linewidth=3, legend=True, legend_kwds={'label': 'Coverage Value', 'orientation': 'horizontal', 'pad': 0.05} ) # 叠加GPS点 polyline_gdf.plot(ax=ax, color='#F50057', markersize=10) plt.title('LineString Coverage Gradient Visualization') plt.tight_layout() plt.show()
小提示:
- 颜色值可以完全按你的喜好调整,比如把深红换成
#FF0000,浅红换成#FFE6E6,蓝系同理 - Folium方案的优势是交互式,用户可以放大缩小查看细节;Matplotlib方案适合生成静态的高清图
- 如果觉得颜色过渡不够顺滑,可以调整
get_gradient_color里的插值逻辑,或者在Matplotlib方案里把N值调大
内容的提问来源于stack exchange,提问作者Tarazali Ryskulov
相关产品推荐
相关产品推荐

