python-visualization / branca

This library is a spinoff from folium, that would host the non-map-specific features.
https://python-visualization.github.io/branca/
MIT License
118 stars 66 forks source link

Adding a custom colored background on a colorbar? #91

Closed lhoupert closed 2 years ago

lhoupert commented 3 years ago

Is there an easy way to add a custom background to a colorbar (e.g. white) to make it more visible?

I would like to add a white rectangle frame below the colorbar . Being able to set the alpha for the frame background could also be nice...

image

lhoupert commented 3 years ago

I forgot to say, I am happy to start working on a PR if I can get some directions :-)

VictordotCC commented 2 years ago

Same issue here, is there any way to change font style or to add a colored background to a linear colormap?

aluthfian commented 2 years ago

I just found out that, if you insert this line

my_map = <a folium map object>
colormap = <a branca element object>

svg_style = '<style>svg#legend {background-color: white;}</style>'
my_map.get_root().header.add_child(folium.Element(svg_style))

colormap.add_to(my_map)

See the example: https://colab.research.google.com/drive/1QC3GYgmFBNFCtF9t0dU9WtC2unrgYcYO?usp=sharing image

Then it will give you the freedom to set the background colour of your colourmap. This is because the branca colourmap is rendered as an SVG (see the colormap._repr_html() result).

Edit: included the legend id, see https://github.com/python-visualization/branca/issues/91#issuecomment-1465198967

Conengmo commented 2 years ago

Thanks for sharing that trick @aluthfian.

If somebody comes along who wants to make a PR for this: look at color_scale.js where the svg is created, here we could add an optional background. https://github.com/python-visualization/branca/blob/482221c1dc5a4b66443a94af0fda91eb529ddef9/branca/templates/color_scale.js#L28. That Javascript file is used here, https://github.com/python-visualization/branca/blob/1a4b7122f3586070d3c10bcdb6383ba671116bec/branca/colormap.py#L76, where we would also then need to add some variable or parameter to set that background.

I'll close the issue for now, since we have a workaround.

vsantalov commented 1 year ago

I just found out that, if you insert this line [...]

The solution works but adding any geojson to your map will make the entire background white. You basically lose the basemap.

Conengmo commented 1 year ago

The solution works but adding any geojson to your map will make the entire background white. You basically lose the basemap.

You can solve that by specifying the legend in the CSS statement:

svg_style = '<style>svg#legend {background-color: white;}</style>'
karina-marques commented 1 year ago

@Conengmo Great, it works. Thanks!