# Makie Contourf Giving Incorrect Whitespace in Contours

**URL:** <https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222>\
**Category:** Visualization\
**Tags:** makie, cairomakie, contouring\
**Created:** [March 6, 2024, 1:32am UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222 "2024-03-06T01:32:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [March 6, 2024, 1:32am UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/1 "2024-03-06T01:32:29Z")

</div>

Hello,

I am trying to plot the initial condition of some simulations I am running. The simulation is two-dimensional and the initial condition is composed of some hyperbolic tangents.

The issue is that when I plot this using `contourf!` from Makie I get an unexpected white band in the middle. My expectation is that everything that is white should be yellow (based on the accompanying picture below).

I’d guess this is an issue with whatever algorithm is generating the contours? There is not enough of a step in that area to generate contours? Something like that?

I’d appreciate any ideas on how to resolve this in Makie (i.e., I don’t want to go to another package or program to generate the plots).

Here is an MWE.

```julia
using CairoMakie, LinearAlgebra

begin
    nx = ny = 10
    xgrid = LinRange(0, 1, nx)
    ygrid = LinRange(0, 1, ny)
    ymin, ymax = 0.4, 0.6
    steepness = 0.1
    f(x,y) = (tanh((y - ymin) / steepness) - tanh((y - ymax) / steepness) - 1)
    z = [f(x,y) for x in xgrid, y in ygrid]
end

begin
    fig = Figure()
    ax = Axis(fig[1, 1], xlabel = "x", ylabel = "y")
    cof = contourf!(xgrid, ygrid, z, colormap=:plasma)
    Colorbar(fig[1, 2], cof)
    fig
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/a/4afc0d04450c5cf326c0af94116e81b2b607ca85.png)

---

<div class="post-metadata">

**Author:** ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)\
**Post date:** [March 6, 2024, 10:35am UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/2 "2024-03-06T10:35:30Z")

</div>

See maybe `extendlow` and/or `extendhigh` in [contourf](https://docs.makie.org/stable/reference/plots/contourf).

```julia-repl
julia> fig, ax, cof = contourf(xgrid, ygrid, z, extendlow=:cyan, extendhigh=:magenta)
julia> Colorbar(fig[1, 2], cof)
julia> fig

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [March 6, 2024, 11:09am UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/3 "2024-03-06T11:09:59Z")

</div>

I think a proper solution would be a fix of [this Makie issue](https://github.com/MakieOrg/Makie.jl/issues/3470): the white region in the plot is a hole in the contour (you can see the grid when it’s not disabled by the theme). I’m surprised the `extend...` fill the hole.

---

<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:** [March 6, 2024, 1:36pm UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/4 "2024-03-06T13:36:00Z")

</div>

I think that’s a bug, the last color in the poly subplot ends up as Inf for some reason, giving it the default NaN color which is transparent. When I manually set that color to the actual max value, I get the correct plot.

```julia
julia> cof.plots[1].color[]
21-element Vector{Float64}:
 -0.9327735900878906
 -0.9327735900878906
 -0.799637496471405
 -0.799637496471405
 -0.6665014028549194
 -0.6665014028549194
 -0.5333653688430786
 -0.5333653688430786
 -0.400229275226593
 -0.400229275226593
 -0.2670931816101074
 -0.2670931816101074
 -0.1339571177959442
 -0.1339571177959442
 -0.000821039080619812
 -0.000821039080619812
  0.1323150396347046
  0.1323150396347046
  0.2654511034488678
  0.2654511034488678
 Inf

```

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

---

<div class="post-metadata">

**Author:** ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)\
**Post date:** [March 6, 2024, 4:51pm UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/5 "2024-03-06T16:51:24Z")

</div>

The bug is that `is_extended_low` and `is_extended_high` are marked `true`, where they shouldn’t [Makie.jl/src/basic\_recipes/contourf.jl at master · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/blob/master/src/basic_recipes/contourf.jl#L104-L105).

I’m taking a look, I guess it’s worth opening an issue.

EDIT: opened [contourf bug · Issue #3683 · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/issues/3683), fix tbd in [fix Inf value on non-extended case by t-bltg · Pull Request #3684 · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/pull/3684).

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [March 7, 2024, 2:04am UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/6 "2024-03-07T02:04:07Z")

</div>

Thanks for doing that.

Do you have any near-term fix suggestions though?

In this particular instance since the high side seems to be getting the `Inf`, maybe I could do something with `cof.nan_color`?

Maybe set it equal to the high side color of the colormap? I can “see” a high side color thing when I do `cof.colormap` but it is some type of Observable object and I don’t know how to access its contents.

Edit: Just tried `cof.nan_color = to_colormap(cof.colormap[])[end]` which does not appear to resolve the issue.

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [March 7, 2024, 2:20am UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/7 "2024-03-07T02:20:23Z")

</div>

> [@jules](#):
>
> When I manually set that color to the actual max value, I get the correct plot.

Can you share how you did that manual fix?

---

<div class="post-metadata">

**Author:** ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)\
**Post date:** [March 7, 2024, 8:35am UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/8 "2024-03-07T08:35:02Z")

</div>

> [@TI36XPro](#):
>
> Do you have any near-term fix suggestions though?

You can use `extendhigh=:auto`.

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [March 8, 2024, 1:15am UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/9 "2024-03-08T01:15:21Z")

</div>

Thanks - that does work. But it adds the little extend high triangle to the colorbar. @jules demonstrated an example above of filling in the correct color without the little extend high triangle. Do you know how that was done? If not I will accept this and wait for the fix.

---

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [March 10, 2024, 4:51pm UTC](https://discourse.julialang.org/t/makie-contourf-giving-incorrect-whitespace-in-contours/111222/10 "2024-03-10T16:51:55Z")

</div>

Thanks for all the replies. I manually fixed it by doing `cof.plots[1].color[][end] = 1.0`.
