Zero at center of Colorbar

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

You have to make the colorrange symmetric:

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

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.


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.

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

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.

1 Like

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:

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

1 Like