# Colorbar for contour plot with uneven spacing of contour lines

**URL:** <https://discourse.julialang.org/t/colorbar-for-contour-plot-with-uneven-spacing-of-contour-lines/114329>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [May 15, 2024, 10:52pm UTC](https://discourse.julialang.org/t/colorbar-for-contour-plot-with-uneven-spacing-of-contour-lines/114329 "2024-05-15T22:52:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkube](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rkube/32/211198_2.png) [@rkube](https://discourse.julialang.org/u/rkube)\
**Post date:** [May 15, 2024, 10:52pm UTC](https://discourse.julialang.org/t/colorbar-for-contour-plot-with-uneven-spacing-of-contour-lines/114329/1 "2024-05-15T22:52:42Z")

</div>

Hi,  
I’m trying to add a colorbar for a contour plot with uneven spacing between the contour lines. The contour plot

```julia
using CairoMakie

fig = let 
    fig = Figure()
    ax = Axis(fig[1, 1])

    xrg = 0.0:0.01:1.0
    yrg = -1.0:0.1:1.0

    z(x, y) = x.^2 .+ y.^2

    zvals = zeros(length(xrg), length(yrg))
    for (ix_x, x) ∈ enumerate(xrg)
        for (ix_y, y) ∈ enumerate(yrg)
            zvals[ix_x, ix_y] = z(x,y)
        end
    end

    cf = contour!(ax, xrg, yrg, zvals, levels=cat(0.1:0.05:0.3, 0.4:0.1:1.0, dims=1), labels=true, colormap=:hsv)
    #Colorbar(fig[1, 2], cf)
    fig
end

```

When I naively try to add a colorbar I get the error

```julia
ERROR: Multiple colormaps found for plot MakieCore.Text{Tuple{Vector{Point{2, Float32}}}}, please specify which one to use manually. Please overload `Makie.extract_colormap(::MakieCore.Text{Tuple{Vector{Point{2, Float32}}}})` to allow for the automatical creation of a Colorbar.

```

I also tried wrapping into a [categorical colormap,](https://docs.makie.org/stable/reference/blocks/colorbar/#experimental_categorical_support), but get the same error.

Is there a good way of adding the colorbar with levels specified in the contour plot?

---

<div class="post-metadata">

**Author:** ![rkube](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rkube/32/211198_2.png) [@rkube](https://discourse.julialang.org/u/rkube)\
**Post date:** [May 15, 2024, 10:57pm UTC](https://discourse.julialang.org/t/colorbar-for-contour-plot-with-uneven-spacing-of-contour-lines/114329/2 "2024-05-15T22:57:32Z")

</div>

This code actually does what I need:

```julia
    fig = Figure()
    ax = Axis(fig[1, 1])

    xrg = 0.0:0.01:1.0
    yrg = -1.0:0.1:1.0

    z(x, y) = x.^2 .+ y.^2

    zvals = zeros(length(xrg), length(yrg))
    for (ix_x, x) ∈ enumerate(xrg)
        for (ix_y, y) ∈ enumerate(yrg)
            zvals[ix_x, ix_y] = z(x,y)
        end
    end

    c_levels = cat(0.0:0.05:0.3, 0.4:0.1:1.0, dims=1)
    cc = cgrad(:matter, c_levels, categorical=true)

    cf = contour!(ax, xrg, yrg, zvals, levels=c_levels, labels=true, colormap=cc)
    Colorbar(fig[1, 2], colormap=cc, ticks=c_levels)
    fig

```

Posting here always gives me the good ideas 🙂

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [May 16, 2024, 2:15am UTC](https://discourse.julialang.org/t/colorbar-for-contour-plot-with-uneven-spacing-of-contour-lines/114329/3 "2024-05-16T02:15:22Z")

</div>

FYI you can use

```julia
 zvals = [z(x,y) for x in xrg, y in yrg]

```

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [May 16, 2024, 2:21am UTC](https://discourse.julialang.org/t/colorbar-for-contour-plot-with-uneven-spacing-of-contour-lines/114329/4 "2024-05-16T02:21:18Z")

</div>

Please open an issue about this in the Makie repo! The initial code should have worked, and since it didn’t it’s a bug.
