Independent custom colorbar in Plots?

Hi, let’s suppose I have the following (essentially empty) plot:

plot(xlabel="x axis", ylabel="y axis")


Now, for some reason, I want to add a colorbar to this figure (yes, the colorbar has nothing to do with the plot… yet).
I just want an independant colorbar, say on the right of the figure, showing some colormap, for example cmap=cgrad(:thermal), with values ranging between 0 and 1.

I found many issues related to the colorbar itself, but I could not understand how to create a custom colormap in Plots.jl. Any help appreciated !

A workaround using layouts:

using Plots; gr()
l = @layout [a{0.95w} b]
cmap = cgrad(:thermal)
p1 = plot(rand(10), xlabel="x axis", ylabel="y axis")
p2 = heatmap(rand(2,2), clims=(0,1), framestyle=:none, c=cmap, cbar=true, lims=(-1,0))
plot(p1, p2, layout=l)

And with GMT

using GMT

C = makecpt(range=(0,1), cmap=:turbo);
plot(rand(10), lc=:blue, xlabel="x axis", ylabel="y axis", colorbar=true, show=true)

If we’re adding how other plotting packages handle this, this is a good example how Makie does these things a bit differently and you add a colorbar explicitly instead of using a plotting command as a workaround:

using CairoMakie

f, ax, sc = lines(rand(10))
cb = Colorbar(f[1, 2], colormap = :turbo, colorrange = (0, 1))

1 Like