Skip to content

Add position option to colormap legends - #246

Open
KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:colormap-legend-position
Open

KhadeerBasha1232 wants to merge 1 commit into
python-visualization:mainfrom
KhadeerBasha1232:colormap-legend-position

Conversation

@KhadeerBasha1232

Copy link
Copy Markdown

Add position option to colormap legends

Summary

The colormap legend is always drawn in the top-right corner of the map, because the template hardcodes L.control({position: 'topright'}). This adds a position argument so the legend can be placed in any corner:

import branca.colormap as cm

colormap = cm.LinearColormap(["yellow", "red"], vmin=0, vmax=100, position="bottomleft")
colormap.add_to(m)  # m is a folium.Map

For a folium Choropleth, the legend can be moved with choropleth.color_scale.position = "bottomleft".

Related issues

This has been requested several times on folium:

Changes

  • ColorMap, LinearColormap and StepColormap take a new position argument: "topright", "topleft", "bottomright" or "bottomleft" (Leaflet's control positions).
  • The default is "topright", so existing maps look the same.
  • Invalid values raise a ValueError listing the allowed positions, instead of silently producing a broken control.
  • Derived colormaps keep their position: to_step(), to_linear() and scale() pass it on.
  • color_scale.js uses {{ this.position|tojson }} instead of the hardcoded 'topright'.

Font size, which #1605 also asks about, is not part of this PR to keep it small.

Tests

  • New tests check, for both LinearColormap and StepColormap, that every position (and the default) ends up in the rendered L.control(...) call.
  • Invalid positions ("top", "TopLeft", "", None) raise ValueError.
  • to_step, to_linear and scale keep the position.
  • tests/test_colormap.py: 30 passed. The rest of the suite also passes locally, apart from tests that need packages I couldn't run on this machine (test_iframe.py needs Selenium; two write_png tests need NumPy).
  • flake8, isort, black, codespell, pyupgrade and add-trailing-comma pass on the changed files, using the versions pinned in .pre-commit-config.yaml.

Checked in a browser

I rendered a folium map for each position and checked where Leaflet placed the legend:

position Leaflet container Legend drawn
topright (default) leaflet-top leaflet-right ✅
topleft leaflet-top leaflet-left ✅
bottomright leaflet-bottom leaflet-right ✅
bottomleft leaflet-bottom leaflet-left ✅

No console errors. On narrow screens the 450px-wide legend can overlap other controls (e.g. the attribution at the bottom right), the same as the top-right legend does today with other top-right controls.

The colormap legend was always added as L.control({position:
'topright'}). ColorMap, LinearColormap and StepColormap now take a
`position` argument ("topright", "topleft", "bottomright" or
"bottomleft"). The default stays "topright", and to_step, to_linear and
scale keep the position of the colormap they're called on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant