# Makie.jl: colormap without coarse graining

**URL:** <https://discourse.julialang.org/t/makie-jl-colormap-without-coarse-graining/81130>\
**Category:** Visualization\
**Tags:** plotting, makie\
**Created:** [May 16, 2022, 6:33am UTC](https://discourse.julialang.org/t/makie-jl-colormap-without-coarse-graining/81130 "2022-05-16T06:33:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Kleinbeck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_kleinbeck/32/21278_2.png) [@Kevin\_Kleinbeck](https://discourse.julialang.org/u/Kevin_Kleinbeck)\
**Post date:** [May 16, 2022, 6:33am UTC](https://discourse.julialang.org/t/makie-jl-colormap-without-coarse-graining/81130/1 "2022-05-16T06:33:04Z")

</div>

**Motivation** I have a function `f` on a 2d domain, which approximatively has a minimal negative value -1 and a maximal positive value of \approx 1/4 (not really important), which I want to visualise in a way, such that f \approx 0 is drawn as white (the function decays exponentially fast, so the background should essentially be white. As an example consider

```julia
xs = -3:0.03:3
ys = xs

f(r) = (sqrt(2)*r^2 - 1) * exp(-r^2)
data = [f(√(x^2 + y^2)) for x ∈ xs, y ∈ ys]

```

**The Problem** is, that Makie.jl seems to coarse-grain any colormap, probably doing a cubic interpolation or something. Now, since white is a corner point in RGB space, this makes it impossible to to get true white as an intermediate point.

Consider following example:

```julia
function make_test_fig(cmaps)
  fig = Figure(resolution = (300 * length(cmaps), 300))

  for i ∈ 1:length(cmaps)
    heatmap(fig[1, i], xs, ys, data, colorrange = (-1.0, 0.25), colormap = cmaps[i])
		
    Colorbar(fig[1, 2i],
      limits = (-1.0, 0.25), ticks = -1.0:0.25:0.25, colormap = cmaps[i]
    )
  end
  fig
end

```

With this I get these result

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

1. Left most image is created with

```julia
ColorScheme(
  [range(colorant"orange", colorant"white", length = 80)...,
  range(colorant"white", colorant"blue", length = 20)...]
)

```

The idea simply was that the interval [-1,0] takes 80% of the total weight of the interval [-1,1/4]. But clearly f \approx 0 has a blue tint.

1. The second idea was then to shift blue a little bit upward

```julia
ColorScheme(
  [range(colorant"orange", colorant"white", length = 82)...,
  range(colorant"white", colorant"blue", length = 18)...]
)

```

Still, this doesn’t seem to work perfectly, is hacky, doesn’t seem to generalise, and most of all: It requires some magic numbers, which are basically guesses

1. Okay, if we cannot bring the prophet to the mountain, we bring the mountain to the prophet. I.e., let’s add manually a large white section in our color bar.

```julia
ColorScheme(
  [range(colorant"orange", colorant"white", length = 78)...,
  fill(colorant"white", 4)...,
  range(colorant"white", colorant"blue", length = 18)...]
)

```

Instead of `fill(colorant"white", 4)` I used `fill(colorant"red", 4)` above to demonstrate whats happening next.

1. So solution 3 seems to technically work, but the white area is quite large (speaking of relative weight in the [-1,1/4] domain) and this is a little bit of a dishonest representation. So let’s at least reduce the weight:

```julia
ColorScheme(
  [range(colorant"orange", colorant"white", length = 79)...,
  fill(colorant"white", 2)...,
  range(colorant"white", colorant"blue", length = 19)...]
)

```

Again, I used `red` above, instead of `white` above for demonstration purposes. Aaaaand we end up with the last figure… So red lies a little bit below the wanted value f \approx 0, and it is clearly washed out. This then motivated my hypothesis: Makie.jl does some cubic interpolation for the colours.

So how can I fix the color value of my colormap at specific values and have nice interpolation _between_ these values. Sadly, Makie’s documentation is no help at all 🥲

In the example I used the packages `CairoMakie`, `Colors`, and `ColorSchemes`

**Addendum** I tried `fill(colorant"red", 1)` to fix to find out how fix the index for the f \approx 0 and I got a figure without any red in it 😅. So apparantly, Makie probably does not even sample the entire provided colormap but instead just picks some values? Why is this no where documented? 😭

---

<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:** [May 16, 2022, 8:57am UTC](https://discourse.julialang.org/t/makie-jl-colormap-without-coarse-graining/81130/2 "2022-05-16T08:57:55Z")

</div>

I think makie samples with linear interpolation from a vector of colors with equal distance between the endpoints of the color range. So if your colormap doesn’t have enough entries and the entry values don’t correspond to specific values you want to see you will get interpolation. You could try increasing the number of colors in the colormap so much that you don’t see interpolation effects anymore.

---

<div class="post-metadata">

**Author:** ![Kevin\_Kleinbeck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_kleinbeck/32/21278_2.png) [@Kevin\_Kleinbeck](https://discourse.julialang.org/u/Kevin_Kleinbeck)\
**Post date:** [May 16, 2022, 9:13am UTC](https://discourse.julialang.org/t/makie-jl-colormap-without-coarse-graining/81130/3 "2022-05-16T09:13:58Z")

</div>

See the addumdum: Your suggestions was one of the things I tried, but sometimes Makie seems just to drop some values and that seems to become worse when you actually increase the number of colors. 🥲 Can provide an example if needed

---

<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 16, 2022, 9:58am UTC](https://discourse.julialang.org/t/makie-jl-colormap-without-coarse-graining/81130/4 "2022-05-16T09:58:29Z")

</div>

> [@Kevin\_Kleinbeck](#):
>
> Makie seems just to drop some values and that seems to become worse when you actually increase the number of colors.

Ok, that sounds like a bug…Out of curiosity, have you tried GLMakie as well?  
Just to pin down if this is a backend bug 😉

---

<div class="post-metadata">

**Author:** ![Kevin\_Kleinbeck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_kleinbeck/32/21278_2.png) [@Kevin\_Kleinbeck](https://discourse.julialang.org/u/Kevin_Kleinbeck)\
**Post date:** [May 16, 2022, 3:48pm UTC](https://discourse.julialang.org/t/makie-jl-colormap-without-coarse-graining/81130/5 "2022-05-16T15:48:51Z")

</div>

@sdanisch GLMakie seems to behave similarily.

 ![FFS](https://global.discourse-cdn.com/julialang/original/3X/2/9/29cd9eab9619266780442c054e74266251c4df16.png)

This figure is with regard to @jules reply. I generated the left most figure with

```julia
[range(colorant"orange", colorant"white", length = 19)...,
fill(colorant"red", 2)...,
range(colorant"white", colorant"blue", length = 4)...]

```

and than subsequently just doubled all numerical values. In the color bars specifically you can see that the red center gets: shifted, narrowed and also “interpolated” or what ever is going on. I produced this figure with GLMakie and CairoMakie
