localtileserver icon indicating copy to clipboard operation
localtileserver copied to clipboard

add support for custom matplotlib colormap

Open 12rambau opened this issue 2 years ago • 6 comments

When displaying single band imagery specifically categorical maps, sometimes you need a very specific color classification with value-color pairs. In the current implementation, only a list of hex colors or a colormap name can be used. would you like to add support for customized matplotlib colormap?

12rambau avatar Jul 24 '22 20:07 12rambau

Should be easy enough!

I believe a custom colormap could be converted to what is currently supported with this:

import matplotlib.colors as mcolors

cmap = ... # custom MPL colormap
color_list = [mcolors.rgb2hex(cmap(i)) for i in range(cmap.N)]

I will add support for any matplotlib.colors.Colormap subclass

banesullivan avatar Jul 25 '22 04:07 banesullivan

from ipyleaflet import Map
from localtileserver import examples
from localtileserver import TileClient, get_leaflet_tile_layer
import matplotlib
import matplotlib.colors as mcolors

b_client = examples.get_bahamas()

cmap = matplotlib.cm.get_cmap('jet', 5)
color_list = [mcolors.rgb2hex(cmap(i)) for i in range(cmap.N)]

# Create ipyleaflet tile layer from that server
t = get_leaflet_tile_layer(b_client, cmap=color_list)

# Create ipyleaflet map, add tile layer, and display
m = Map(center=b_client.center(), zoom=b_client.default_zoom)
m.add_layer(t)
m

banesullivan avatar Jul 25 '22 05:07 banesullivan

sounds about right. So you "export" all the values contained in the matplotlib colormap. I guess that in my use case I'll need to be extra careful wih vmin and vmax to make sure that values are corresponding right ?

12rambau avatar Jul 25 '22 06:07 12rambau

Yes, we have to “export” the colors like this and yes, you’ll need to be concious of the vmin/vmax.

I’m curious… how are you defining your colormaps? Do they have a range associated with them? If you could share, I may be able to implement something that would handle all this under the hood.

banesullivan avatar Jul 25 '22 06:07 banesullivan

I need to find back a meaningful example, let me check in my files and I'll share something. In the meantime I've implemented your suggestion

12rambau avatar Jul 25 '22 06:07 12rambau

@12rambau, following up on this: do you have an example you could share? I'd like to support this either here or directly in large-image

banesullivan avatar Aug 29 '22 14:08 banesullivan

At this time, I don't think I'll be able to add support for this. I have completely redesigned localtileserver from the ground up to now entirely rely on rasterio and rio-tiler and it seems the infrastucture is best suited for named matplotlib colormaps.

PRs welcome in the future to implement this, but I don't have the bandwidth.

One solution could be to create your own package of colormaps and register them with MPL in the environment (like cmocean) then refer to them by name

banesullivan avatar Jan 21 '24 22:01 banesullivan