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

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:

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,

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

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.

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?

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.


Would that be what you want as a default?
Or would a colormap without the gaps be more desirable?

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.