# CairoMakie contourf set colour limits

**URL:** <https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [April 9, 2022, 3:08pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261 "2022-04-09T15:08:00Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![jewh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jewh/32/28586_2.png) [@jewh](https://discourse.julialang.org/u/jewh)\
**Post date:** [April 9, 2022, 3:08pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/1 "2022-04-09T15:08:00Z")

</div>

How do I change the limits for my colour scale in a CairoMakie `contourf` plot?

For instance say I want to plot my data on a contour plot, with a colour bar:

```julia
using CairoMakie

M = rand(10, 10) / 10. # get data ∈ [0, 1] and <<1 to illustrate
fig = CairoMakie.Figure()
ax = Axis(fig[1,1])
cplot = CairoMakie.contourf!(fig[1,1], 1:10, 1:10, M)
Colorbar(fig[1,2], cplot)
fig

```

 ![test1](https://global.discourse-cdn.com/julialang/original/3X/c/7/c7e7551eae66903b7b050765bb5afe569cfe6166.png)

How can I change the colour limits here?

Passing `colorrange=(0,1)` as an argument to `countourf` doesn’t work:

```julia
fig = CairoMakie.Figure()
ax = Axis(fig[1,1])
cplot = CairoMakie.contourf!(fig[1,1], 1:10, 1:10, M, colorrange=(0,1))
Colorbar(fig[1,2], cplot)
fig

```

 ![test1](https://global.discourse-cdn.com/julialang/original/3X/c/7/c7e7551eae66903b7b050765bb5afe569cfe6166.png)

Passing `levels=0:0.1:1` as an argument to `contourf` works, but it creates this ugly white border around the plot:

```julia
fig = CairoMakie.Figure()
ax = Axis(fig[1,1])
cplot = CairoMakie.contourf!(fig[1,1], 1:10, 1:10, M, levels=0:0.1:1)
Colorbar(fig[1,2], cplot)
fig

```

 ![test3](https://global.discourse-cdn.com/julialang/original/3X/9/b/9bdca71eafb5bfce4ca118401c811f51f561c367.png)

How can I adjust the colour range without creating a white border?

---

<div class="post-metadata">

**Author:** ![jewh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jewh/32/28586_2.png) [@jewh](https://discourse.julialang.org/u/jewh)\
**Post date:** [April 9, 2022, 3:09pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/2 "2022-04-09T15:09:42Z")

</div>

Worth noting that the white border is also present in examples of `Colorbar` on the Makie website: ([link](https://makie.juliaplots.org/stable/examples/layoutables/colorbar/))

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [April 9, 2022, 3:51pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/3 "2022-04-09T15:51:36Z")

</div>

I’ll admit its very far from an elegant solution, but you can of course always set your axes limits manually, after you use `levels=0:0.1:1`. To make it more generalized, it looks like we can get the correct range from the fields of cplot:

```julia

xrange = cplot.input_args[1].val #returns 1:10 here
yrange = cplot.input_args[2].val #returns 1:10 here
xlims!(ax,extrema(xrange))
ylims!(ax,extrema(yrange))

```

Still, there should be a better solution to this imo. (Why should there even be a white border in this case, that looks like unintended behaviour to me)

---

<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:** [April 9, 2022, 4:18pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/4 "2022-04-09T16:18:15Z")

</div>

`tightlimits!(ax)` removes the white border

---

<div class="post-metadata">

**Author:** ![jewh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jewh/32/28586_2.png) [@jewh](https://discourse.julialang.org/u/jewh)\
**Post date:** [April 9, 2022, 4:29pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/5 "2022-04-09T16:29:37Z")

</div>

Excellent, thank you both.

---

<div class="post-metadata">

**Author:** ![jewh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jewh/32/28586_2.png) [@jewh](https://discourse.julialang.org/u/jewh)\
**Post date:** [April 9, 2022, 4:46pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/6 "2022-04-09T16:46:48Z")

</div>

[Opened an issue for it](https://github.com/JuliaPlots/Makie.jl/issues/1812), at the very least might get an answer as to whether it’s necessary for something

---

<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:** [April 9, 2022, 5:29pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/7 "2022-04-09T17:29:11Z")

</div>

I think such an issue was already raised the other day… The reason behind it is that I think no margin around a rectangular contourf plot looks better, so that’s the default. But if custom levels are specified, these often result in curvy shapes where not all the data are included. So I made it such that the limits don’t tighten in that case. The heuristic could definitely be improved.

---

<div class="post-metadata">

**Author:** ![jewh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jewh/32/28586_2.png) [@jewh](https://discourse.julialang.org/u/jewh)\
**Post date:** [April 9, 2022, 5:46pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/8 "2022-04-09T17:46:18Z")

</div>

Do you have an example of such curvy shapes? Struggling to visualise the issue

---

<div class="post-metadata">

**Author:** ![jewh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jewh/32/28586_2.png) [@jewh](https://discourse.julialang.org/u/jewh)\
**Post date:** [April 9, 2022, 5:47pm UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/9 "2022-04-09T17:47:07Z")

</div>

Ah, didn’t see the issue when browsing! Do you have a link to it / issue #? I’ll close my issue as a duplicate

---

<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:** [April 10, 2022, 5:05am UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/10 "2022-04-10T05:05:05Z")

</div>

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

[https://makie.juliaplots.org/stable/examples/plotting\_functions/contourf/#relative\_mode](https://makie.juliaplots.org/stable/examples/plotting_functions/contourf/#relative_mode)

---

<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:** [April 10, 2022, 5:09am UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/11 "2022-04-10T05:09:51Z")

</div>

Ah maybe it was just on Slack then

---

<div class="post-metadata">

**Author:** ![jewh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jewh/32/28586_2.png) [@jewh](https://discourse.julialang.org/u/jewh)\
**Post date:** [April 10, 2022, 9:07am UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/12 "2022-04-10T09:07:35Z")

</div>

Hmm, why do the axes automatically rescale in the left plot? Feel it makes more sense to plot on the same axes as the original plot, then let the user adjust the axes with `xlims!` and `ylims!`.

Think this’d dodge the present issue because the plot area specified by `levels` is only ever a subset of the plot area shown in the original plot, right? Therefore all the plot specified by `levels` is guaranteed to be in this plot area, and no need to handle curvy shapes etc.

---

<div class="post-metadata">

**Author:** ![jewh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jewh/32/28586_2.png) [@jewh](https://discourse.julialang.org/u/jewh)\
**Post date:** [April 10, 2022, 9:10am UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/13 "2022-04-10T09:10:10Z")

</div>

Additionally, is `levels` the only way to control the colour limits?

---

<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:** [April 10, 2022, 11:12am UTC](https://discourse.julialang.org/t/cairomakie-contourf-set-colour-limits/79261/14 "2022-04-10T11:12:16Z")

</div>

No the limits are always auto-determined when you plot (unless you fix them via `limits!`, `xlims!`, `ylims!`), the question is just if the margins are also tight or not, and that happens only for a few special plot types like contourf, heatmap, image.
