I am trying to make a contour plot with Makie. I would like to have custom labels on the lines. So instead of the number on the contour, it displays some text (particularly the variable I have as customLabels).
using CairoMakie
using LaTeXStrings
function energyContour(; N=2, h=1, EsmA = 0, a=1)
E(k) = EsmA + 2*h*( cos( k[1]*a ) + cos(k[2]*a) )
testX = [ pi/a*j/N for j in -N:N]
xx = [ [ testX[i] , testX[j] ] for i in 1:length(testX), j in 1:length(testX)]
Es = E.(xx)
println(xx)
println(Es)
levels = [ EsmA+2*h , EsmA+h , EsmA , EsmA-2*h ]
customLabels = [L"E-A+2h" , L"E-A + h", L"E-A ", L"E-A -2h" ]
fig, ax, ct = contour(testX,testX,Es;levels=levels, labels= true )
return fig
end
The code above gives this output, but I would like instead of [-2,0,1,2] it to say customLabels

You can use the labelformatter kwarg of contour. labelformatter is a function from contour value to display string. Below is an example, which doesn't use too much intelligence for numeric formatting (and in particular will probably not do the right thing for numbers whose magnitude is small or large enough that Julia uses scientific formatting to stringify them) but should get the idea across.
function labelformatter(value)
h_term = if value == -1
"-h"
elseif value == 0
""
elseif value == 1
"+h"
elseif value > 0
"+$(value)h"
else # < 0
"$(value)h"
end
return "E-A$(h_term)"
end
# all same as above except for this line
fig, ax, ct = contour(testX, testX, Es; levels=levels, labels=true, labelformatter)