# How to construct a categorical colormap with one specific value corresponding to a specific color?

**URL:** <https://discourse.julialang.org/t/how-to-construct-a-categorical-colormap-with-one-specific-value-corresponding-to-a-specific-color/126114>\
**Category:** Visualization\
**Tags:** question, plotting, makie, cairomakie\
**Created:** [February 20, 2025, 1:32pm UTC](https://discourse.julialang.org/t/how-to-construct-a-categorical-colormap-with-one-specific-value-corresponding-to-a-specific-color/126114 "2025-02-20T13:32:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![thudjx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thudjx/32/15231_2.png) [@thudjx](https://discourse.julialang.org/u/thudjx)\
**Post date:** [February 20, 2025, 1:32pm UTC](https://discourse.julialang.org/t/how-to-construct-a-categorical-colormap-with-one-specific-value-corresponding-to-a-specific-color/126114/1 "2025-02-20T13:32:11Z")

</div>

# What I want to do:

I have an array of intergers that ranges over `-3:2`, which I want to plot with heatmap. The difficult part is that I want the `0` to be ploted by some “neutral” color, say grey in the colormap [`:coolwarm`](https://docs.juliahub.com/MakieGallery/Ql23q/0.2.17/generated/colors.html#matplotlib).

# what I have tried

```julia
let
    Random.seed!(10)
    n = 10
    x = 1:n
    y = fill(1, n)
    cn = rand(-3:2, n)
    cnmin, cnmax = minimum(cn), maximum(cn)
    colormap1 = cgrad(:coolwarm, 2 * abs(cnmin) + 1, categorical=true)[1:cnmax-cnmin+1]
    colormap2 = cgrad(colormap1, cnmax-cnmin+1, categorical=true)
    @show length(colormap1)
    @show length(colormap2)
    fig = Figure()
    ax = Axis(fig[1, 1])
    heatmap!(ax, x, y, cn, colormap=colormap1, colorrange=(cnmin, cnmax))
    Colorbar(fig[2, 1], colormap=colormap1, ticklabelsize=25,
             limits=(cnmin-0.5, cnmax+0.5), ticks=cnmin:cnmax,
             vertical=false, flipaxis=false, height=20, width=Relative(0.65), labelsize=40)
    Colorbar(fig[3, 1], colormap=colormap2, ticklabelsize=25,
             limits=(cnmin-0.5, cnmax+0.5), ticks=cnmin:cnmax,
             vertical=false, flipaxis=false, height=20, width=Relative(0.65), labelsize=40)
    fig
end

```

# my questions

1. why I have to use `cgrad` twice to get the real categorical colormap?
2. `length(colormap1) == length(colormap2) == 5`, but when `colormap1` and `colormap2` is plotted in colormap, they are so different, why?
3. Is there a better way to do this?
