Makie: nonlinear color levels in Colorbar

ryofurue
Sun, 11 Aug 2024 14:24:39 +0000
Visualization

In a contourf map, I sometimes need to show the structure of the field at small absolute values as well as showing large absolute values. At the same time, I’d like to enable the reader to “read” approximate values off the map. For example, the first map near the bottom of this message is the sea-surface temperature anomaly of January from the annual average. The contour levels are -2, -1, -0.5, -0.2, 0.2, 0.5, 1, 2 as the color bar shows. You can easily sea which region has which temperature value range.

On the other hand, the following sample program generates the second attached plot. The contour intervals are exactly as intended, but the colorbar remains “linear” unlike in the first plot.

using CairoMakie

xs = 0.0:1.0:10.0
ys = 0.0:1.0:7.0

func(x,y) = exp((x^2 + y^2)/20)
vals = func.(xs, ys')
levels = [2, 5, 10, 20, 50, 100, 200]

fig = Figure()
ax = Axis(fig[1,1])
c = contourf!(ax, xs, ys, vals,
              levels=levels, extendlow=:auto, extendhigh=:auto)
Colorbar(fig[1,2],c)
save("tmp2.png", fig)

It’s not easy to see which value range each color represents.

Is there an option to Makie to achieve a nonlinear colorbar easily?

The method I’ve thought of so far is very tedious:

# Take log of the field for negative values and positive values
logtemp = (x -> (x > eps) ? log(x) : (x < -eps) ? log(-x) : 0.0).(temp)
# Plot log(temp)
c = contourf!( . . . , logtemp, levels = . . . )
# Manually put labels like "-0.2" on the colorbar
Colormap(  . . . )

The log part would be easy, but calculating the labels on the colorbar would be tedious.

6 posts - 3 participants