How to combine two different colormaps into one

I come across the following colorbar somewhere and would like to implement something similar for my heatmap plot in CairoMakie:

image

So suppose I want ColorSchemes.viridis from -5 to -2, and then ColorSchemes.Greys_3 from -2 to 0, how should I combine the two?

1 Like

Does this work for you?

using GLMakie
using ColorSchemes
GLMakie.activate!()

fig = Figure(resolution = (800, 800));
ax = Axis(fig[1,1])
xs = LinRange(0, 10, 100);
ys = LinRange(0, 15, 100);
zs = [ -5 * cos(x) * sin(y) for x in xs, y in ys]

# color bar ratios
cmin, cmiddle, cmax = -5, -2, 0
clength = cmax - cmin
ratio_viridis = (cmiddle - cmin) / clength
ratio_greys3  = (cmax - cmiddle) / clength

# construct a colormap
viridis = get(ColorSchemes.viridis, LinRange(0,1,Int64(ratio_viridis*100)))
greys3 = get(ColorSchemes.Greys_3, LinRange(0,1,Int64(ratio_greys3*100)))
virgre3 = vcat(viridis, greys3)

h = heatmap!(ax, xs, ys, zs, colormap=virgre3, colorrange=(cmin, cmax))

Colorbar(fig[1,2], h)

fig

3 Likes