You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自定义Folium色条标题文本?增大色阶标题字体大小

解决Folium色条标题字体过小的问题

Folium默认的色条标题样式确实偏小,你可以通过以下几种方式调整字体大小:

方法1:直接修改标题的HTML样式

创建LinearColormap时,把caption替换成带内联样式的HTML标签,直接指定字体大小:

import folium
import folium.colormap as cm

# 假设df是你的数据框
colormap = cm.LinearColormap(
    colors=['green','red'], 
    index=[min(df['output']), max(df['output'])], 
    vmin=min(df['output']),
    vmax=max(df['output']), 
    # 用span标签设置字体大小,可根据需求调整px值
    caption='<span style="font-size: 16px; font-weight: 600;">output in units</span>'
)

# 务必将色条添加到地图
colormap.add_to(m)

# 你的圆形标记代码保持不变
for _, row in df.iterrows():
    folium.Circle(
        location=[row['Latitude'], row['Longitude']], 
        radius=800*row["output"],
        fill=True,
        color=colormap(row["output"])
    ).add_to(m)

方法2:全局修改色条标题的CSS样式

如果需要统一调整所有色条的标题样式,给地图添加自定义CSS规则,强制覆盖默认样式:

# 创建色条(用普通文本作为caption即可)
colormap = cm.LinearColormap(
    colors=['green','red'], 
    index=[min(df['output']), max(df['output'])], 
    vmin=min(df['output']),
    vmax=max(df['output']), 
    caption='output in units'
)
colormap.add_to(m)

# 添加自定义CSS到地图
m.get_root().html.add_child(folium.Element("""
    <style>
        /* 针对Folium色条的标题元素 */
        .folium-colorbar .caption {
            font-size: 18px !important;
            font-weight: bold;
            /* 还可添加其他样式,比如颜色、间距 */
            color: #333;
            margin-top: 5px;
        }
    </style>
"""))

# 圆形标记代码同上

方法3:修改色条的HTML源码

如果前两种方法不生效,直接修改色条生成的HTML内容,替换默认样式:

colormap = cm.LinearColormap(
    colors=['green','red'], 
    index=[min(df['output']), max(df['output'])], 
    vmin=min(df['output']),
    vmax=max(df['output']), 
    caption='output in units'
)

# 获取色条原始HTML,替换caption的样式
original_html = colormap._repr_html_()
modified_html = original_html.replace(
    '<div class="caption">', 
    '<div class="caption" style="font-size: 16px; font-weight: bold;">'
)

# 将修改后的HTML添加到地图
from folium.elements import CustomElement
CustomElement(modified_html).add_to(m)

注意:无论用哪种方法,都要确保把色条通过add_to(m)添加到地图上,否则标题不会显示。

内容的提问来源于stack exchange,提问作者Ajay Ganesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 11:32:38