Makie - tricontourf from matplotlib in Makie

I did some further work on this to be clear about what I tried. Maybe I just didn’t do it right - happy to be enlightened!

As agreed, I posted this also as an issue to Makie:

using TriplotBase, Triangulate, GLMakie, ColorSchemes

function append_with_nan!(a,b)
    append!(a,b)
    push!(a,NaN)
end

conttype(contour::TriplotBase.Contour{T}) where {T} = T
#create some data
x = [0.0, 0.5, 1.0, 0.25, 0.75, 0.5]
y = sqrt(3)/2*[0.0, 0.0, 0.0, 0.5, 0.5, 1.0]
z = [0.0, 0.0, 0.0, 1.0, 0.75, 0.75]

#figure and axes
fighandle = Figure(resolution = (900, 900))
ax2 = Axis(fighandle[1, 1], aspect = AxisAspect(2/sqrt(3)))
ax3 = Axis(fighandle[1, 2], aspect = AxisAspect(2/sqrt(3)))
ax4 = Axis(fighandle[2, 1], aspect = AxisAspect(2/sqrt(3)))
ax5 = Axis(fighandle[2, 2], aspect = AxisAspect(2/sqrt(3)))

#trianulate datapoints
triin=Triangulate.TriangulateIO()
triin.pointlist=[x'; y']
(triout, vorout) = triangulate("Q", triin)
trianglelist = triout.trianglelist

#contours
contours = TriplotBase.tricontour(x, y, z, trianglelist, 15)
filledcontours = TriplotBase.tricontourf(x, y, z, trianglelist, 15)

#plot the contours in ax2 (code adapted to Makie.jl from TriplotRecipes.jl (which is for Plots.jl))
for contour=contours
    T = conttype(contour)
    xs = T[]
    ys = T[]
    zs = T[]
    for polyline=contour.polylines
        append_with_nan!(xs,first.(polyline))
        append_with_nan!(ys,last.(polyline))
        append!(zs,fill(contour.level,length(polyline)))
    end
    if !isempty(zs)
        lines!(ax2, xs, ys, color = zs, colorrange = (0.0, 1.0), colormap = :magma)
    end
end

#try getting filled contours into ax3 (code adapted to Makie.jl from TriplotGR.jl) - works, but just with cycled colors.
for filledcontour = filledcontours
    for polyline=filledcontour.polylines
        points = similar(polyline, Point2f) #this is probably horrible Julia, but it works for now.
        for i in axes(polyline)
            points[i] = polyline[i]
        end
        poly!(ax3, points)
    end
end

#try getting filled contours into ax4 (code adapted to Makie.jl from TriplotGR.jl) - try coloring contours properly by specifying color, colorrange and colormap.
#--> results in an empty plot
lmin,lmax = extrema(getfield.(filledcontours,:lower))
for filledcontour = filledcontours
    for polyline=filledcontour.polylines
        points = similar(polyline, Point2f) #this is probably horrible Julia, but it works for now.
        for i in axes(polyline)
            points[i] = polyline[i]
        end
        poly!(ax4, points, color = filledcontour.lower, colorrange = (lmin, lmax), colormap = :magma)
    end
end

#try getting filled contours into ax5 (code adapted to Makie.jl from TriplotGR.jl) - try coloring contours by specifying colors directly
#--> results in just one contour shown with one color - why?
colors = colorschemes[:magma]
lmin,lmax = extrema(getfield.(filledcontours,:lower))
for filledcontour = filledcontours
    for polyline=filledcontour.polylines
        points = similar(polyline, Point2f) #this is probably horrible Julia, but it works for now.
        for i in axes(polyline)
            points[i] = polyline[i]
        end
        poly!(ax5, points, color = colors[div(255*filledcontour.lower-lmin,lmax-lmin)+1])
    end
end

Resulting plot:

1 Like