Repository navigation
Add position option to colormap legends - #246
Open
KhadeerBasha1232 wants to merge 1 commit into
Open
KhadeerBasha1232 wants to merge 1 commit into
KhadeerBasha1232 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
positionoption to colormap legendsSummary
The colormap legend is always drawn in the top-right corner of the map, because the template hardcodes
L.control({position: 'topright'}). This adds apositionargument so the legend can be placed in any corner:For a folium
Choropleth, the legend can be moved withchoropleth.color_scale.position = "bottomleft".Related issues
This has been requested several times on folium:
ColorMapandcolor_scale.js.StepColormapat the top of the map.Changes
ColorMap,LinearColormapandStepColormaptake a newpositionargument:"topright","topleft","bottomright"or"bottomleft"(Leaflet's control positions)."topright", so existing maps look the same.ValueErrorlisting the allowed positions, instead of silently producing a broken control.to_step(),to_linear()andscale()pass it on.color_scale.jsuses{{ 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
LinearColormapandStepColormap, that every position (and the default) ends up in the renderedL.control(...)call."top","TopLeft","",None) raiseValueError.to_step,to_linearandscalekeep 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.pyneeds Selenium; twowrite_pngtests need NumPy)..pre-commit-config.yaml.Checked in a browser
I rendered a folium map for each position and checked where Leaflet placed the legend:
positiontopright(default)leaflet-top leaflet-righttopleftleaflet-top leaflet-leftbottomrightleaflet-bottom leaflet-rightbottomleftleaflet-bottom leaflet-leftNo 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.