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

**URL:** <https://discourse.julialang.org/t/makie-jl-plotting-contour-with-fillrange-true-above-lines/39830>\
**Category:** New to Julia\
**Tags:** package, plotting\
**Created:** [May 20, 2020, 1:43pm UTC](https://discourse.julialang.org/t/makie-jl-plotting-contour-with-fillrange-true-above-lines/39830 "2020-05-20T13:43:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [May 20, 2020, 1:43pm UTC](https://discourse.julialang.org/t/makie-jl-plotting-contour-with-fillrange-true-above-lines/39830/1 "2020-05-20T13:43:25Z")

</div>

Hello,

I am trying to learn how to use Makie and found myself in a situation where I would like to plot some lines and a contour plot with filllrange=true (or also a heatmap) on the same scene while having the contour plot appear in front of the lines.  
The contour plot does not fully overlap the lines and I would then like to have the contour “hide” the lines below it in regions where they overlap.

Here is a simplified example of what I am trying to do.

```julia
using Makie, AbstractPlotting

np = 500
nc = 200
f(x) = 2*x+1
x = LinRange(-1,1,f(np))
y = rand(f(np))*2 .- 1
scene = lines(x,y)
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
contour!(scene,x_c,y_c,c_mat,fillrange=true)

```

I have been trying playing with transparency or with the plot order but I can’t find anyway to avoid the lines coming on top of the contour plot.  
Is the thing I am trying to achieve even possible with Makie at the moment?

---

<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:** [May 20, 2020, 2:38pm UTC](https://discourse.julialang.org/t/makie-jl-plotting-contour-with-fillrange-true-above-lines/39830/2 "2020-05-20T14:38:19Z")

</div>

Like so?  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/1/11383fe21fa12c9d4114aab2dd759d5e34184955.png)

```julia
using AbstractPlotting

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
heatmap!(scene,x_c,y_c,c_mat)
lines!(scene,x, y)
contour!(scene,x_c,y_c,c_mat, linewidth=2, color=:white)

```

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [May 20, 2020, 5:19pm UTC](https://discourse.julialang.org/t/makie-jl-plotting-contour-with-fillrange-true-above-lines/39830/3 "2020-05-20T17:19:30Z")

</div>

Hi @sdanisch

No, what I would like to obtain is for the black line to be completely invisible if passing below the area where the contour plot is defined.

Something like this where instead of the white square the contour would appear

 ![test](https://global.discourse-cdn.com/julialang/original/3X/c/1/c1300c230b62e4ee72a8930fb59e81ff76097386.png)

With poly! instead of heatmap! this happens automatically respecting the plot order but with heatmap and contour I can’t seem to make it work

---

<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:** [May 20, 2020, 5:30pm UTC](https://discourse.julialang.org/t/makie-jl-plotting-contour-with-fillrange-true-above-lines/39830/4 "2020-05-20T17:30:43Z")

</div>

I see.  
Seems to be a z-fighting issue. Try:

```julia
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)
contour!(scene,x_c,y_c,c_mat, linewidth=2, color=:white) 

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/4/243f07b0c4648356b5f7afc318343282260f55d3.jpeg)

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [May 20, 2020, 5:46pm UTC](https://discourse.julialang.org/t/makie-jl-plotting-contour-with-fillrange-true-above-lines/39830/5 "2020-05-20T17:46:48Z")

</div>

Thanks a lot!  
That solved it.  
And also thanks for the amazing work in Makie, it is truly an outstanding plotting package :).

Now I just have a last curiosity regarding contour plots.  
Is there a way also label the contour lines based on the levels?  
Something like here where you can see the 16 on the upper left contour:  
 ![Annotation 2020-05-20 194358](https://global.discourse-cdn.com/julialang/original/3X/b/1/b1b97d556c1eb06b703309b110864d8aeb293e9f.png)

The above picture was of course generated with PlotlyJS but I am trying to migrate to Makie.

---

<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:** [May 20, 2020, 10:24pm UTC](https://discourse.julialang.org/t/makie-jl-plotting-contour-with-fillrange-true-above-lines/39830/6 "2020-05-20T22:24:10Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/3/2/320044b0db30cb31d949fe82f12ffcaabe95973b.jpeg)

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

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

```julia
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 😉 (e.g. simply split the contour lines to make a gap, and finding better positions for the numbers)

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

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [May 21, 2020, 2:22pm UTC](https://discourse.julialang.org/t/makie-jl-plotting-contour-with-fillrange-true-above-lines/39830/7 "2020-05-21T14:22:37Z")

</div>

Nice!  
I think I can work from this and also learn something more about Makie.  
One last question, are the actual contour levels stored somehow in the plot data?  
Right now the numbers on the contour represent the level index while I wanted to put there the actual iso-value for which the contour is plotted.
