Using only a part of a colormap in Makie

I have a figure where I am plotting a line and want that line to be colored according to some other array. How do I use only part of a colormap? Or even reverse a sub-section of a colormap?

I don’t have a MWE, but the relevant parts of my code are below:

nodalplot = nothing
for coordsi in coords_list
    global nodalplot
    nodalplot = lines!(ax, coordsi[:, 1], coordsi[:, 2], coordsi[:, 3]; color=freqnodal, linewidth=10, colormap=:brg)
end
Colorbar(fig[1, 2], nodalplot)

So for example, I only want to use the blue → red section of the brg colormap. I can set the colorrange while plotting the lines, but it doesn’t allow me to change the limits of the Colorbar.

nodalplot = lines!(ax, coordsi[:, 1], coordsi[:, 2], coordsi[:, 3]; color=freqnodal, linewidth=10, colormap=:brg, colorrange=(0.315, 0.35))
Colorbar(fig[1, 2], nodalplot)

Trying to pass a colorrange arugment into Colorbar gives the error:

ERROR: Both colorrange + limits are set, please only set one, they're aliases. colorrange: (0.315, 0.35), limits: (0.315, 0.35)

whereas passing a limits argument gives the error:

ERROR: You should not pass the `limits` attribute to the colorbar when constructing it using an existing plot object. This attribute is copied from the plot object, and setting it from the colorbar will make the plot object and the colorbar go out of sync.
1 Like

You can make your own colormap with e.g. cgrad, and use it with colormap=.... following the documentation:

https://docs.juliaplots.org/latest/generated/colorschemes/#ColorGradient

example code:

using GLMakie
GLMakie.activate!()

cm = cgrad([:blue, :red])

xs = LinRange(0, 20, 50)
ys = LinRange(0, 15, 50)
zs = [cos(x) * sin(y) for x in xs, y in ys]

fig = Figure()

ax, hm = heatmap(fig[1, 1], xs, ys, zs, colormap=cm)
Colorbar(fig[1, 2], hm)

fig

You can also extract part of an existing colormap, they’re just vectors of colors after all

How do I extract the colors from an existing colormap?

By indexing into it, like Makie.to_colormap(:name)[1:10]