Makie.jl: Plotting contour with fillrange=true above lines

Not as easily… I think @jules did some work on this?

I just did a super dirty hack for a simple version:

using AbstractPlotting, GLMakie

np = 500
nc = 200
f(x) = 2*x+1
x = LinRange(-1,1,f(np))
y = rand(f(np))*2 .- 1
scene = Scene()
x_c = LinRange(-nc/np,nc/np,f(nc))
y_c = LinRange(-nc/np,nc/np,f(nc))
c_mat = @. x_c^2 + y_c'^2
lineplot = lines!(scene, x, y)[end]
translate!(lineplot, 0, 0, -1)
heatmap!(scene,x_c,y_c,c_mat)
cplot = contour!(scene,x_c,y_c,c_mat, linewidth=2, color=:white)[end]

beginnings = Point2f0[]; colors = RGBAf0[]
# First plot in contour is the line plot, first arguments are the points of the contour
segments = cplot.plots[1][1][]
for (i, p) in enumerate(segments)
    # the segments are separated by NaN, which signals that a new contour starts
    if isnan(p)
        push!(beginnings, segments[i-1])
    end
end
sc = scatter!(scene, beginnings, markersize=0.06, color=(:white, 0.00001))[end]
translate!(sc, 0, 0, 1)
# Reshuffle the plot order, so that the scatter plot gets drawn before the line plot
delete!(scene, sc)
delete!(scene, cplot)
push!(scene, sc)
push!(scene, cplot)
anno = annotations!(scene, [(string(i), p) for (i, p) in enumerate(beginnings)], 
                    textsize=0.05, align=(:center, :center))[end]
                    
# move, so that text is in front
translate!(anno, 0, 0, 2)

scene

Would need some polish to work nicely :wink: (e.g. simply split the contour lines to make a gap, and finding better positions for the numbers)

2 Likes