# Colarbar: equal-sized color rectangles

**URL:** <https://discourse.julialang.org/t/colarbar-equal-sized-color-rectangles/99932>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [June 6, 2023, 10:34am UTC](https://discourse.julialang.org/t/colarbar-equal-sized-color-rectangles/99932 "2023-06-06T10:34:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thunderclouds](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@thunderclouds](https://discourse.julialang.org/u/thunderclouds)\
**Post date:** [June 6, 2023, 10:34am UTC](https://discourse.julialang.org/t/colarbar-equal-sized-color-rectangles/99932/1 "2023-06-06T10:34:41Z")

</div>

How do I create a color bar with the same height for each level?  
Default scales rectangles according to the levels.

 ![a](https://global.discourse-cdn.com/julialang/original/3X/9/4/9457b5c923200ebe17300ebe5f411b2ae50551bf.png)

```julia
using CairoMakie

xs = LinRange(0, 20, 50)
ys = LinRange(0, 15, 50)
zs = [cos(x) * sin(y) for x in xs, y in ys]

fig = Figure()

ax, hm = contourf(fig[1, 1], xs, ys, zs,
    colormap = :Spectral, levels = [0, 0.1, 0.2, 0.5, 1])
Colorbar(fig[1, 2], hm, ticks = [0, 0.1, 0.2, 0.5, 1])

save("a.png", fig)

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [June 6, 2023, 11:07am UTC](https://discourse.julialang.org/t/colarbar-equal-sized-color-rectangles/99932/2 "2023-06-06T11:07:30Z")

</div>

This colorbar syntax should also work in Makie [ColorSchemes · Plots](https://docs.juliaplots.org/stable/generated/colorschemes/#ColorGradient)

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 6, 2023, 11:36am UTC](https://discourse.julialang.org/t/colarbar-equal-sized-color-rectangles/99932/3 "2023-06-06T11:36:02Z")

</div>

Try:

```julia
using CairoMakie

xs = LinRange(0, 20, 50)
ys = LinRange(0, 15, 50)
zs = [cos(x) * sin(y) for x in xs, y in ys]

fig = Figure()

ax, hm = contourf(fig[1, 1], xs, ys, zs,
    colormap = :Spectral, levels = [0, 0.1, 0.2, 0.5, 1])

# this line has changed...
Colorbar(fig[1, 2]; colormap = cgrad(:Spectral, 5; categorical=true), limits=(0.0,1.0), ticks = 0.1:0.2:0.9, tickformat= (x->string.([0,0.1,0.3,0.4,1])))

save("a.png", fig)

```

When passing a contour plot to Colorbar it automatically places the ticks and values according to it - and in this case, not exactly as desired. The call in the above call bypasses this default by calling ColorBar directly and specifying more parameters.

---

<div class="post-metadata">

**Author:** ![thunderclouds](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@thunderclouds](https://discourse.julialang.org/u/thunderclouds)\
**Post date:** [June 6, 2023, 1:47pm UTC](https://discourse.julialang.org/t/colarbar-equal-sized-color-rectangles/99932/5 "2023-06-06T13:47:26Z")

</div>

Thanks for the advice.  
Based on your advice, I was able to do what I wanted to do with the following code.

```julia
using CairoMakie

xs = LinRange(0, 20, 50)
ys = LinRange(0, 15, 50)
zs = [cos(x) * sin(y) for x in xs, y in ys]

fig = Figure()

ax, hm = contourf(fig[1, 1], xs, ys, zs,
    colormap = :Spectral, levels = [0, 0.1, 0.2, 0.5, 1])
Colorbar(fig[1, 2]; colormap = cgrad(:Spectral, 4; categorical=true), limits=(0.0,1.0), ticks = 0.0:0.25:1.0, tickformat= (x->string.([0,0.1,0.2,0.5,1])))

save("a.png", fig)

```

 ![a](https://global.discourse-cdn.com/julialang/original/3X/d/1/d112dacc89b264f9694e18d1a711295add88dc3f.png)
