# Zero at center of Colorbar

**URL:** <https://discourse.julialang.org/t/zero-at-center-of-colorbar/82664>\
**Category:** General Usage\
**Tags:** plotting, math, plots, makie\
**Created:** [June 13, 2022, 8:11am UTC](https://discourse.julialang.org/t/zero-at-center-of-colorbar/82664 "2022-06-13T08:11:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![markub3327](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markub3327/32/36784_2.png) [@markub3327](https://discourse.julialang.org/u/markub3327)\
**Post date:** [June 13, 2022, 8:11am UTC](https://discourse.julialang.org/t/zero-at-center-of-colorbar/82664/1 "2022-06-13T08:11:32Z")

</div>

Hello,

I use the `:balance` colorscheme with the ideally zero as white at the centre of `Colorbar`. The highest value is red and the lowest value is blue. If I have a range (-4.725205, 28.010359), I get around 10 at the centre of `Colorbar`, but I need to zero is still in the centre and everything highest or lowest will be interpolated to the red or blue. How can I fix the centre of `Colorbar` to zero?

![Snímka obrazovky 2022-06-13 o 9.58.09](https://global.discourse-cdn.com/julialang/original/3X/3/5/35e1758495579f9aa199272f56cdea4bb78cbdcd.png)  
The left image is an example of `colorrange = (-4.725205, 28.010359)` and the centre is calculated as 11,642577, what is I don’t want.  
The right image is example with zero in the centre, but it’s not fixed by programmer, it’s only calculated as (-17.564026, 22.240145) =\> 22.240145 - (-17.564026) = 39,804171 =\> 39,804171 / 2 = 19,9020855 =\> **The centre is 2,3380595**.

Thanks.

---

<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:** [June 13, 2022, 10:18am UTC](https://discourse.julialang.org/t/zero-at-center-of-colorbar/82664/2 "2022-06-13T10:18:28Z")

</div>

You have to make the colorrange symmetric:

```julia
data = randn(30, 30) .+ 1
f, ax, hm = heatmap(data,
    colormap = :balance,
    colorrange = (-1, 1) .* maximum(abs, data))
Colorbar(f[1, 2], hm)
f

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/4/4/44f2a00bf826b26c1d89985bbf4beb3f6fb46580.png)

---

<div class="post-metadata">

**Author:** ![markub3327](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markub3327/32/36784_2.png) [@markub3327](https://discourse.julialang.org/u/markub3327)\
**Post date:** [June 13, 2022, 11:05am UTC](https://discourse.julialang.org/t/zero-at-center-of-colorbar/82664/3 "2022-06-13T11:05:48Z")

</div>

It’s not precisely what I talking about, but it works. The main disadvantage is when the changes in values are too small. It makes the `colorrange` so huge and can hide these small changes in values.

 ![Snímka obrazovky 2022-06-13 o 13.04.51](https://global.discourse-cdn.com/julialang/original/3X/1/8/18142c0ecd1720b5905297ba3380dd30b1b3878a.png)  
 ![Snímka obrazovky 2022-06-13 o 12.58.31](https://global.discourse-cdn.com/julialang/original/3X/5/2/52778ca883462142512646db6cfbd49aceac8160.png)

The first image is original.  
The second image is after applied own calculation of the range.

On the other side, the symmetry is so good. I have the three scenarios with values. Only negative, only positive and combined. The zero is the common point for each of them.

```julia
function set_colormap!(hm)
        if hm.colorrange[][1] >= 0
            hm.colorrange[] = (0, 1) .* maximum(abs, hm.colorrange[])
            hm.colormap = :amp
        else
            if hm.colorrange[][2] > 0
                hm.colorrange[] = (-1, 1) .* maximum(abs, hm.colorrange[])
                hm.colormap = :balance
            else
                hm.colorrange[] = (-1, 0) .* maximum(abs, hm.colorrange[])
                hm.colormap = :ice
            end
        end

        println(hm.colorrange[][1], ", ", hm.colorrange[][2])
end

```

---

<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:** [June 13, 2022, 12:07pm UTC](https://discourse.julialang.org/t/zero-at-center-of-colorbar/82664/4 "2022-06-13T12:07:48Z")

</div>

Ok that is a different problem, yes if you need to have zero in your colormap and don’t use any nonlinear scale, you will have hard to discriminate values if they are far away from zero and close together.

---

<div class="post-metadata">

**Author:** ![markub3327](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markub3327/32/36784_2.png) [@markub3327](https://discourse.julialang.org/u/markub3327)\
**Post date:** [June 13, 2022, 12:25pm UTC](https://discourse.julialang.org/t/zero-at-center-of-colorbar/82664/5 "2022-06-13T12:25:50Z")

</div>

Why not? I can use a nonlinear scale.

Only requirements:

Zeros = white color  
Maximal values = red color  
Minimal (only negative) values = blue color

Values between -100 and 0 or 0 and 100 can be nonlinearly scaled.

You see it here:

 ![Snímka obrazovky 2022-06-13 o 14.28.55](https://global.discourse-cdn.com/julialang/original/3X/5/7/57f03b32d88b7fa11c25b590e6a5df031a8aab36.png)

The first one has zero as white, but the second has 5 as white and the last one has 30 as white. These plots must be comparable with each other !!!

Here is the own scale by `set_colormap!(hm)`:

 ![Snímka obrazovky 2022-06-13 o 14.36.48](https://global.discourse-cdn.com/julialang/original/3X/9/4/9452989cf70b5713ade215fd8cf1155f024ccf03.png)
