# Center colorbar ticks in a categorical colorbar in Makie.jl

**URL:** <https://discourse.julialang.org/t/center-colorbar-ticks-in-a-categorical-colorbar-in-makie-jl/63691>\
**Category:** Visualization\
**Tags:** question, makie\
**Created:** [June 28, 2021, 11:33am UTC](https://discourse.julialang.org/t/center-colorbar-ticks-in-a-categorical-colorbar-in-makie-jl/63691 "2021-06-28T11:33:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [June 28, 2021, 11:33am UTC](https://discourse.julialang.org/t/center-colorbar-ticks-in-a-categorical-colorbar-in-makie-jl/63691/1 "2021-06-28T11:33:06Z")

</div>

MWE:

```julia
using GLMakie

xg = range(0, 10; length = 100)
yg = range(0, 10; length = 100)
bsn = rand(1:5, (100, 100))

fig = Figure(resolution = (700,600))
display(fig)

colors = [:black, :blue, :red, :teal, :yellow]

cmap = cgrad(colors, 5; categorical = true)
# cmap = cgrad(COLORSCHEME[1:length(att)]; categorical = true)

ax = fig[1,1] = Axis(fig)
heatob = Observable(bsn[:, :, 1])
hm = heatmap!(ax, xg, yg, heatob; colormap = cmap)
hm.colorrange = (1, 5)
cbar = fig[:, 2] = Colorbar(fig, hm, label = "attractor #")

```

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

The question is: how to make the ticks of the colorbar centered exactly at the middle of each color segment. Example:

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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 28, 2021, 12:10pm UTC](https://discourse.julialang.org/t/center-colorbar-ticks-in-a-categorical-colorbar-in-makie-jl/63691/2 "2021-06-28T12:10:24Z")

</div>

The problem is that you have 5 distinct values for a 5 value colormap, but you choose a colorrange from 1 to 5. A 5 value colormap has 6 edges, so your edges are `[1.0, 1.8, 2.6, 3.4, 4.2, 5.0]`. If you have categorical values, make them the centers of each colormap bin. So choose the colorrange as `(0.5, 5.5)`, then your edges will be `[0.5, 1.5, 2.5, 3.5, 4.5, 5.5]` with the centers 1 to 5.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [June 28, 2021, 12:54pm UTC](https://discourse.julialang.org/t/center-colorbar-ticks-in-a-categorical-colorbar-in-makie-jl/63691/3 "2021-06-28T12:54:15Z")

</div>

Thanks, this is already helpful.

I may have found a bug when animating such a colormap. It seems that if I set the ticks manually, they are still updated during animating. MWE:

```julia
using GLMakie

xg = range(0, 10; length = 100)
yg = range(0, 10; length = 100)
bsn = rand(1:3, (100, 100))
bsn2 = rand(3:5, (100, 100))
bsn = cat(bsn, bsn2; dims = 3)

fig = Figure(resolution = (700,600))
display(fig)

colors = [:black, :blue, :red, :teal, :yellow]
cmap = cgrad(colors, 5; categorical = true)

ax = fig[1,1] = Axis(fig)
heatob = Observable(bsn[:, :, 1])
hm = heatmap!(ax, xg, yg, heatob; colormap = cmap)
hm.colorrange = (1, 5)
cbar = fig[:, 2] = Colorbar(fig, hm, label = "attractor #")
hm.colorrange = (0.5, 5.5)
cbar.ticks = 1:5

record(fig, "test.mp4", 1:size(bsn)[3]; profile = "high") do i
    heatob[] = bsn[:, :, i]
    tit[] = "Slice $i"
end

```

once you produce this you will see that one frame has ticks 1,2,3 and the other 3,4,5. If I add the `hm.colorrange = (1, 5)` and `cbar.ticks = 1:5` inside the animation loop it’s okay, but I would expect that if I manually set tics/ranges, then they wouldn’t subsequently change.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [June 28, 2021, 1:49pm UTC](https://discourse.julialang.org/t/center-colorbar-ticks-in-a-categorical-colorbar-in-makie-jl/63691/4 "2021-06-28T13:49:13Z")

</div>

This seems to work for me, maybe the `hm.colorrange` isn’t picked up correctly when it’s done after the fact? That would be a bug I think.

```julia
       xg = range(0, 10; length = 100)
       yg = range(0, 10; length = 100)
       bsn = rand(1:3, (100, 100))
       bsn2 = rand(3:5, (100, 100))
       bsn = cat(bsn, bsn2; dims = 3)

       fig = Figure(resolution = (700,600))
       display(fig)

       colors = [:black, :blue, :red, :teal, :yellow]
       cmap = cgrad(colors, 5; categorical = true)

       tit = Node("title")
       ax = fig[1,1] = Axis(fig, title = tit)
       heatob = Observable(bsn[:, :, 1])
       hm = heatmap!(ax, xg, yg, heatob; colormap = cmap, colorrange = (0.5, 5.5))

       cbar = fig[:, 2] = Colorbar(fig, hm, label = "attractor #")
       cbar.ticks = 1:5

       record(fig, "test.mp4", 1:size(bsn)[3]; profile = "high") do i
           heatob[] = bsn[:, :, i]
           tit[] = "Slice $i"
       end

```

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [June 28, 2021, 1:52pm UTC](https://discourse.julialang.org/t/center-colorbar-ticks-in-a-categorical-colorbar-in-makie-jl/63691/5 "2021-06-28T13:52:45Z")

</div>

Can confirm: if I give the colorrange at the _creation_ point, it works.
