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

如何在Folium中设置颜色条位置?将其从右上移至左上

解决方案

要在Python脚本中调整Folium颜色条的位置,无需手动修改HTML文件,你可以通过folium.plugins.ColorMapControl控件直接指定颜色条位置,具体操作如下:

  1. 先导入所需插件:
from folium.plugins import ColorMapControl
  1. 保留你原本的颜色映射创建代码,然后用ColorMapControl替代默认的颜色条添加方式,设置position='topleft'即可将颜色条移到左上:
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')

# 遍历添加圆形标记(你的原有代码,注意补充循环逻辑)
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)

# 添加颜色条到左上位置
ColorMapControl(colormap=colormap, position='topleft').add_to(m)

position参数支持的取值包括:'topleft'(左上)、'topright'(右上,默认)、'bottomleft'(左下)、'bottomright'(右下),可根据需求自由选择。

内容的提问来源于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 12:56:03