# Makie - tricontourf from matplotlib in Makie

**URL:** <https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [March 10, 2022, 1:41pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685 "2022-03-10T13:41:22Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmarcotte](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cmarcotte](https://discourse.julialang.org/u/cmarcotte)\
**Post date:** [March 10, 2022, 1:41pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/1 "2022-03-10T13:41:22Z")

</div>

Hi everyone,  
I am trying to get something like tricontourf from matplotlib to work in Makie. The `heatmap` with three vector inputs example [here](https://makie.juliaplots.org/stable/examples/plotting_functions/heatmap/index.html#three_vectors) is the closest I can find in the documentation, but there’s no obvious way to get the spatial interpolation. Trying `contourf` with three input vectors fails, as it expects a matrix for the third. Has anyone implemented something like the `tricontourf` function from matplotlib in Makie?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 10, 2022, 3:55pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/2 "2022-03-10T15:55:09Z")

</div>

Can you please provide an example of the expected output with tricontourf? Most of us here don’t have a Matlab license.

---

<div class="post-metadata">

**Author:** ![cmarcotte](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cmarcotte](https://discourse.julialang.org/u/cmarcotte)\
**Post date:** [March 10, 2022, 3:58pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/3 "2022-03-10T15:58:45Z")

</div>

Hi, I think you misread. I am referring to the python package matplotlib, whose `tricontourf` can be seen [here](https://matplotlib.org/stable/plot_types/unstructured/tricontourf.html). There’s nothing about Matlab; I am not sure Matlab even has an analogous function (I certainly could not find it in the docs).

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [March 10, 2022, 4:28pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/4 "2022-03-10T16:28:41Z")

</div>

I faced exactly this problem recently and cooked up a workaround involving Triangulate.jl and TriplotBase.jl

I’ll post more details later when I am back on my computer with the codes on it.

---

<div class="post-metadata">

**Author:** ![cmarcotte](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cmarcotte](https://discourse.julialang.org/u/cmarcotte)\
**Post date:** [March 10, 2022, 4:55pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/5 "2022-03-10T16:55:22Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [March 10, 2022, 7:35pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/6 "2022-03-10T19:35:27Z")

</div>

GMT’s [contourf](https://www.generic-mapping-tools.org/GMT.jl/dev/gallery/contourf/contourf/) does exactly that.

---

<div class="post-metadata">

**Author:** ![cmarcotte](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cmarcotte](https://discourse.julialang.org/u/cmarcotte)\
**Post date:** [March 11, 2022, 1:05pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/7 "2022-03-11T13:05:03Z")

</div>

Hi, is there some example code you could share that worked for you?

---

<div class="post-metadata">

**Author:** ![cmarcotte](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@cmarcotte](https://discourse.julialang.org/u/cmarcotte)\
**Post date:** [March 11, 2022, 1:05pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/8 "2022-03-11T13:05:25Z")

</div>

I am unfamiliar with GMT; is it built on top of Makie?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [March 11, 2022, 2:04pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/9 "2022-03-11T14:04:56Z")

</div>

No.  
The link I posted is from the GMT.jl manual

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [March 11, 2022, 4:16pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/10 "2022-03-11T16:16:49Z")

</div>

Sorry for the delay in providing the example. I somehow had trouble coloring the filled contours, but here goes:

```julia

#tricontour working example
using TriplotBase, Triangulate, GLMakie

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 = (1100, 900))
ax1 = Axis3(fighandle[1, 1], aspect = (1, sqrt(3)/2, 1))
ax2 = Axis(fighandle[1, 2], aspect = AxisAspect(2/sqrt(3)))
ax3 = Axis(fighandle[1, 3], aspect = AxisAspect(2/sqrt(3)))

scatter!(ax1, x, y, z, markersize = 2000, color = :black)

#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))
    end
end

#plot the filled contours in ax3 (code adapted to Makie.jl from TriplotGR.jl)

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

```

Currently produces this plot:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/5/050cb43bfd150352db95eca1f36815f66769fd3c.png)

I somehow have trouble getting Makie.jl accept the “color” argument for poly! right now…so currently the righthand figure just cycles through the color…

But, it’s pretty close?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [March 11, 2022, 4:28pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/11 "2022-03-11T16:28:23Z")

</div>

Can you open an issue with a minimal reproducable example?

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [March 11, 2022, 4:30pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/12 "2022-03-11T16:30:45Z")

</div>

I’ll try 🙂

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [March 11, 2022, 10:03pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/13 "2022-03-11T22:03:55Z")

</div>

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:

```julia
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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/1/71860521e1fff90207e4704b6d6d5ff5d0147d92.png)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [August 17, 2022, 12:06pm UTC](https://discourse.julialang.org/t/makie-tricontourf-from-matplotlib-in-makie/77685/14 "2022-08-17T12:06:06Z")

</div>

I’ve started to turn this approach into a PR here [Tricontourf plot by jkrumbiegel · Pull Request #2226 · JuliaPlots/Makie.jl · GitHub](https://github.com/JuliaPlots/Makie.jl/pull/2226)
