# Heatmap using categorical colours

**URL:** <https://discourse.julialang.org/t/heatmap-using-categorical-colours/99717>\
**Category:** Visualization\
**Tags:** makie, cairomakie\
**Created:** [June 1, 2023, 9:59am UTC](https://discourse.julialang.org/t/heatmap-using-categorical-colours/99717 "2023-06-01T09:59:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)\
**Post date:** [June 1, 2023, 9:59am UTC](https://discourse.julialang.org/t/heatmap-using-categorical-colours/99717/1 "2023-06-01T09:59:13Z")

</div>

I plot a heatmap of as

```julia
using CairoMakie
x=[1; 2; 1; 2; 501; 500; 501; 500]
f = Figure()
ax = Axis(f[1,1])
hm = heatmap!(ax, ones(8), 1:8, x, colormap=cgrad(:viridis, 4, categorical=true))
Colorbar(f[:, 2], hm)
f

```

Can I have a colour for each value in `x`?

---

<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 1, 2023, 10:02am UTC](https://discourse.julialang.org/t/heatmap-using-categorical-colours/99717/2 "2023-06-01T10:02:48Z")

</div>

Colormaps are for linear lookup, so it’s easiest for you to transform your data to category indices and then change the labels on the colormap accordingly.

---

<div class="post-metadata">

**Author:** ![pascal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pascal/32/207922_2.png) [@pascal](https://discourse.julialang.org/u/pascal)\
**Post date:** [June 1, 2023, 10:08am UTC](https://discourse.julialang.org/t/heatmap-using-categorical-colours/99717/3 "2023-06-01T10:08:44Z")

</div>

Thanks @jules . By category indices you mean something like 1 → 1, 2 → 2, 500 → 3, 501 → 4?

---

<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 1, 2023, 10:18am UTC](https://discourse.julialang.org/t/heatmap-using-categorical-colours/99717/4 "2023-06-01T10:18:37Z")

</div>

Yes exactly. The colorrange can then be set to `min-0.5, max+0.5` so you have one tick in the middle of each color block.
