# CairoMakie contourf set colour limits

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:

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

How can I change the colour limits here?

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

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

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

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

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

Worth noting that the white border is also present in examples of `Colorbar` on the Makie website: (link)

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:

``````
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)

1 Like

`tightlimits!(ax)` removes the white border

2 Likes

Excellent, thank you both.

Opened an issue for it, at the very least might get an answer as to whether it’s necessary for something

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.

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

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

Ah maybe it was just on Slack then

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.

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

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.