# How to combine two different colormaps into one

**URL:** <https://discourse.julialang.org/t/how-to-combine-two-different-colormaps-into-one/87529>\
**Category:** Visualization\
**Tags:** colors, makie, colorbar\
**Created:** [September 20, 2022, 5:29pm UTC](https://discourse.julialang.org/t/how-to-combine-two-different-colormaps-into-one/87529 "2022-09-20T17:29:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yhchang96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yhchang96/32/36499_2.png) [@yhchang96](https://discourse.julialang.org/u/yhchang96)\
**Post date:** [September 20, 2022, 5:29pm UTC](https://discourse.julialang.org/t/how-to-combine-two-different-colormaps-into-one/87529/1 "2022-09-20T17:29:26Z")

</div>

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

![image](https://global.discourse-cdn.com/julialang/original/3X/3/f/3ff50cd51dc96f6362e68ca6ac2a72e5be930b6b.png)

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?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [September 20, 2022, 7:40pm UTC](https://discourse.julialang.org/t/how-to-combine-two-different-colormaps-into-one/87529/2 "2022-09-20T19:40:45Z")

</div>

Does this work for you?

```julia
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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/7/775f6679f7abf64f27028151e153a9a00916fdcd.png)
