Plots.jl, colorbar tick labels

Hi everyone,
I was wondering whether there is a possibility to define colorbar tick labels manually with Plots.j?
By this, I mean something like this:

The possibility of passing a Dict that maps numerical values to text labels as colorbar tick labels could be quite handy (if it is not already possible :D).
cheers!

Not exactly what you have asked for, but just in case, Plots.jl allows displaying a colorbar title that might help. Illustration using the pyplot() backend:

Thanks, that’s already quite interesting.
Below is a MWE. Basically, I thought it would be nice to use the Dict flag_colors to annotate the colorbar. Not sure whether this is possible…

using Plots

# Data
nx, ny = 31, 31
tab = fill(1, nx, ny)
tab[Int(ceil(nx/3)):end,:]   .= 2
tab[Int(ceil(2*nx/3)):end,:] .= 3

# Color palette
cmap    = zeros(RGB{Float64}, 3)
cmap[1] = RGB{Float64}(  6/255,  7/255, 9/255)  # black
cmap[2] = RGB{Float64}(233/255,214/255, 64/255) # yellow
cmap[3] = RGB{Float64}(198/255, 34/255, 49/255) # red

# Labels
labels      = ("Black", "Red", "Yellow")
flag_colors = Dict( 1:3 .=> labels )

display(heatmap(tab', c=palette(cmap,3)) )

For the pyplot() backend of Plots.jl please check this posted issue. Copying the author @isentropic for feedback.

2 Likes

Fyi, here is a possible solution using Plots.jl:

Edited code
using Plots; gr()

# Data
nx, ny = 31, 31
tab = fill(1, nx, ny)
tab[Int(ceil(nx/3)):end,:]   .= 2
tab[Int(ceil(2*nx/3)):end,:] .= 3

# Color palette
cmap    = zeros(RGB{Float64}, 3)
cmap[1] = RGB{Float64}(  6/255,  7/255, 9/255)  # black
cmap[2] = RGB{Float64}(233/255,214/255, 64/255) # yellow
cmap[3] = RGB{Float64}(198/255, 34/255, 49/255) # red

# Labels
labels      = ("Black", "Yellow", "Red")
flag_colors = Dict( 1:3 .=> labels )

p1 = heatmap(tab', c=palette(cmap,3), colorbar=false, framestyle=:none)

data2 = collect(range(extrema(tab')..., 100))
sdic = sort(flag_colors, by=first)
p2 = heatmap([1], data2, [data2;;], colorbar=false, c=palette(cmap,3), xaxis=false, tick_direction=:out,
             ymirror=true, yticks=(keys(sdic), values(sdic))
)

l = @layout [a{0.95w} b]
plot(p1, p2, layout=l)