# Colorbar with function range for a 3D contour plot using GLMakie

**URL:** <https://discourse.julialang.org/t/colorbar-with-function-range-for-a-3d-contour-plot-using-glmakie/103388>\
**Category:** Visualization\
**Tags:** question, plotting, makie, glmakie\
**Created:** [August 30, 2023, 9:03pm UTC](https://discourse.julialang.org/t/colorbar-with-function-range-for-a-3d-contour-plot-using-glmakie/103388 "2023-08-30T21:03:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olegg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olegg/32/51316_2.png) [@Olegg](https://discourse.julialang.org/u/Olegg)\
**Post date:** [August 30, 2023, 9:03pm UTC](https://discourse.julialang.org/t/colorbar-with-function-range-for-a-3d-contour-plot-using-glmakie/103388/1 "2023-08-30T21:03:19Z")

</div>

I’m trying to add a colorbar to a 3D contour plot in GLMakie with the actual range of the function values. However, the colorbar I get only shows a range from 0 to 1, while the actual range is [-0.7, 0.7].

Here is the code I am using:

```julia
using GLMakie

x = LinRange(-1, 1, 20)
y = LinRange(-1, 1, 20)
z = LinRange(-1, 1, 20)
values = [sin(x[i]) * cos(y[j]) * sin(z[k]) for i in 1:20, j in 1:20, k in 1:20]

fig = Figure()
ax = Axis3(fig[1, 1], title="3D Contour Example")

contour!(ax, x, y, z, values, levels=10, colormap=:viridis)

fig[1, 2] = Colorbar(fig, label="Function Value")

display(fig)

```

The alternatives that I tried give errors. For example,

```julia
cf = contour!(ax, x, y, z, levels=50)
fig[1, 2] = Colorbar(fig, cf, label="Function Value")

```

ERROR: KeyError: key :highclip not found  
Stacktrace:  
[1] getindex(h::Dict{Symbol, Observable}, key::Symbol)  
@ Base ./dict.jl:484  
[2] getindex  
@ ~/.julia/packages/MakieCore/zgrOY/src/attributes.jl:89 [inlined]  
[3] getindex(x::Combined{Makie.contour, Tuple{LinRange{Float64, Int64}, LinRange{Float64, Int64}, LinRange{Float64, Int64}, Array{Float32, 3}}}, key::Symbol)  
@ MakieCore ~/.julia/packages/MakieCore/zgrOY/src/attributes.jl:185  
[4] getproperty(x::Combined{Makie.contour, Tuple{LinRange{Float64, Int64}, LinRange{Float64, Int64}, LinRange{Float64, Int64}, Array{Float32, 3}}}, key::Symbol)  
@ MakieCore ~/.julia/packages/MakieCore/zgrOY/src/attributes.jl:76  
[5] Colorbar(fig\_or\_scene::Figure, plot::Combined{Makie.contour, Tuple{LinRange{Float64, Int64}, LinRange{Float64, Int64}, LinRange{Float64, Int64}, Array{Float32, 3}}}; kwargs::Base.Pairs{Symbol, String, Tuple{Symbol}, NamedTuple{(:label,), Tuple{String}}})  
@ Makie ~/.julia/packages/Makie/Ylh0P/src/makielayout/blocks/colorbar.jl:39  
[6] top-level scope  
… my stuff

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/1/418ffc26c4dd47f0aea293d41688a38c04d4ae3e.jpeg)

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [August 31, 2023, 7:43am UTC](https://discourse.julialang.org/t/colorbar-with-function-range-for-a-3d-contour-plot-using-glmakie/103388/2 "2023-08-31T07:43:21Z")

</div>

> [@Olegg](#):
>
> `fig[1, 2] = Colorbar(fig, cf, label="Function Value")`

that is the way to do it. This is a bug, please open an issue.

For now now you could do

` fig[1, 2] = Colorbar(fig, label="Function Value", colorrange=(-0.7,0.7))`

but it not ideal.

---

<div class="post-metadata">

**Author:** ![Olegg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olegg/32/51316_2.png) [@Olegg](https://discourse.julialang.org/u/Olegg)\
**Post date:** [August 31, 2023, 8:57am UTC](https://discourse.julialang.org/t/colorbar-with-function-range-for-a-3d-contour-plot-using-glmakie/103388/3 "2023-08-31T08:57:22Z")

</div>

Thanks, I’ll open an issue later today with the workaround.

Might it, however, be misleading when GLMakie automatically sets the contour levels? If the actual levels are eg between -0.5 and 0.5, then the colorrange of (-0.7, 0.7) could imply incorrect colours.

How would I find out the actual min and max contour levels used, so as to set the colorrange correctly?

---

<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:** [September 1, 2023, 6:26pm UTC](https://discourse.julialang.org/t/colorbar-with-function-range-for-a-3d-contour-plot-using-glmakie/103388/4 "2023-09-01T18:26:16Z")

</div>

This is a temporary regression because the internal handling of colormaps was improved, but handling of nested recipes and their colormaps was not fully solved. In principle you should not need to set limits yourself, as you said, they can easily be wrong then. If you set a colorrange manually, it should usually be on the plot object directly, then the colorbar picks it up from there.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [September 8, 2023, 5:05pm UTC](https://discourse.julialang.org/t/colorbar-with-function-range-for-a-3d-contour-plot-using-glmakie/103388/5 "2023-09-08T17:05:46Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/1/3/138440ded1a10df53155cf8200f70e06786affc5.jpeg)  
Would that be what you want as a default?  
Or would a colormap without the gaps be more desirable?

---

<div class="post-metadata">

**Author:** ![Olegg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/olegg/32/51316_2.png) [@Olegg](https://discourse.julialang.org/u/Olegg)\
**Post date:** [September 19, 2023, 12:25pm UTC](https://discourse.julialang.org/t/colorbar-with-function-range-for-a-3d-contour-plot-using-glmakie/103388/6 "2023-09-19T12:25:04Z")

</div>

I guess a colormap without gaps is more common in applications (eg in MatLab, Plotly etc.). But the one with gaps would be a useful option, and much better than the status quo.
