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
110 stars 63 forks source link

Feature request: branca colormap accepted as a parameter ImageOverlay #128

Closed fedro86 closed 1 year ago

fedro86 commented 1 year ago

Code Sample

import folium
import folium.plugins
import numpy as np

import branca
import branca.colormap as cm

data = np.random.rand(538,892)*100

colormap = cm.LinearColormap(colors=['red','lightblue'],
                             index=[50],
                             vmin=0,vmax=100)

map = folium.Map(location=[50.5039, 4.4699], zoom_start=12)
folium.raster_layers.ImageOverlay(
    image=data,
    bounds=[[53.804418158693835, -0.9254649843200244], [47.42086714394442, 9.65841513678867]],
    opacity=0.7,
    crs='EPSG4326',
    colormap=colormap         
).add_to(map)

map.add_child(colormap)

Problem description Code above creates a raster map over Central Europe. Unfortunately the only possibility to make it colored it is through a matplotlib.colors that however doesn't allow to define vmin vmax.

Expected output By using the branca.colormap LinearColormap and StepColormaap as a parameter in ImageOverlay this issue should be solved and one should be able to define both colormap and color range for any raster.

Output of folium.version folium 0.14.0

Conengmo commented 1 year ago

Seems like this already works?

colormap = linear.viridis.scale(0, 100)
ImageOverlay(
    image=data,
    bounds=[[53.804418158693835, -0.9254649843200244], [47.42086714394442, 9.65841513678867]],
    opacity=0.7,
    crs='EPSG4326',
    colormap=colormap
).add_to(m)

This has been fixed recently, fix is not yet in the latest release, but available on the main branch. See https://github.com/python-visualization/folium/issues/1571#issuecomment-1400081656