Skip to content

coloring issue with poly!() - attempt to implement drawing (filled) contours on triangulated pointlists #1744

@thomvet

Description

@thomvet

Tried to implement the functionality to draw contours on list of points that do not necessarily belong to a structured grid, but have issues with coloring filled contours. It works fine when I just cycle through the colors, but when I try to actually give the correct colors to poly!(), it just fails for some reason.

Related discourse thread: https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/10

Below is a MWE illustrating the issue on a very simple dataset:

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

Results in the following output:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions