# Makie: nonlinear color levels in Colorbar

**URL:** <https://discourse.julialang.org/t/makie-nonlinear-color-levels-in-colorbar/118056>\
**Category:** Visualization\
**Tags:** question, makie, colorbar\
**Created:** [August 11, 2024, 2:24pm UTC](https://discourse.julialang.org/t/makie-nonlinear-color-levels-in-colorbar/118056 "2024-08-11T14:24:39Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [August 12, 2024, 2:55am UTC](https://discourse.julialang.org/t/makie-nonlinear-color-levels-in-colorbar/118056/3 "2024-08-12T02:55:58Z")

</div>

> [@Datseris](#):
>
> The replies I’ve gotten at this post will help you make a nonlinear / nonuniform color scale I believe 🙂

Thanks! [this answer](https://discourse.julialang.org/t/custom-divergent-colormap-with-unequal-sides/115484/8) of @jules’s is closest to what I want to achieve: _You scale the color scheme_ using the parameter

```julie
colorscale=ReversibleScale(conversion_func, inverse_conversion_func)

```

to `contourf`. I paste a my sample code after incorporating the idea.

_The only remaining issue is the labeling of the colorbar._ The labels are still placed at a linear interval (it’s 50 in the following example), missing finer contour levels.

It would be nice if the tickmarks of `Colorbar` tried to follow the levels specified as `levels=[. . .]`.

I’ll investigate.

```julia
using CairoMakie

xs = 0.0:1.0:10.0
ys = 0.0:1.0:7.0

func(x,y) = exp((x^2 + y^2)/20)
vals = func.(xs, ys')
levels = [2, 5, 10, 20, 50, 100, 200]

scaling = let thr = 2.0, lthr = log(thr)
  function scale(x)
    (x > thr) ? log(x) : ((x < -thr) ? -log(-x) : (x/thr)*lthr)
  end
  function invscale(x)
    (x > lthr) ? exp(x) : ((x < -lthr) ? -exp(-x) : (x/lthr)*thr)
  end
  CairoMakie.ReversibleScale(scale, invscale)
end

fig = Figure()
ax = Axis(fig[1,1])
c = contourf!(ax, xs, ys, vals; colorscale=scaling
              ,levels = levels
              ,extendlow=:auto, extendhigh=:auto)
Colorbar(fig[1,2],c)
save("tmp2.png", fig)

```

 ![tmp2](https://global.discourse-cdn.com/julialang/original/3X/c/a/ca9cdf28f2941708d736be08fd113c91df88f6af.png)  
j

---

_[View the full topic](https://discourse.julialang.org/t/makie-nonlinear-color-levels-in-colorbar/118056)._
