julialogarithmmakie.jl

Logarithmic scale for contourf colorbar in Julia using Makie.jl


How do you deal with logarithmic scale in contourf plots in Julia? (using Makie.jl) I just want to plot a two-dimensional data file with values that range from 10 to 10000 and use a logarithmic colorbar, but I can't find how to do it. I have seen that there is an open question about this topic with heatmap but I couldn't find anything about contourf.

Let's say I have field v

julia> v
7×7 Array{Int64,2}:
    2   600    50     2   600    50     2
   50  7000  5000    50  7000  5000    50
  300    20    60   300    20    60   300
 5000     3    70  5000     3    70  5000
   70   150  1000    70   150  1000    70
 1000  8000     2  1000  8000     2  1000
 3000   500     1  3000   500     1  3000

If I try to plot this field with Makie and contourf

julia> fig, ax, im = contourf(1:7, 1:7, v, levels=0:50:10000)
julia> Colorbar(fig[1,2], im)
julia> save("./plot.png", fim)

I get this: Figure 1

My question is how can I use a logarithmic scale in both colormap and colorbar ticks in order to visualize the the differences between small values...


Solution

  • The best solution I came up with is transforming your values into the log space. Here it goes:

    using CairoMakie
    
    # Generate a tool field
    X1, X2 = 0:.1:5, 0:.1:5
    X3 = [ 10 ^ x1 for x1 in X1, x2 in X2 ]
    lvls = 10 .^ collect( X1 )
    
    # Note: we apply log10 to field and levels!
    fig = Figure()
    ax = Axis( fig[1, 1] )
    ctf = contourf!( ax, X1, X2, log10.( X3 ), levels = log10.( lvls[1:5:end] ) )
    
    # We make the ticks string (non-log) match the ticks values (log)
    ticks_val = log10.( lvls[1:5:end] )
    ticks_str = string.( round.(lvls[1:5:end]; digits = 2) )    # Here we can even write by hand a nicer string that has exponents
    Colorbar(fig[1,2], ctf, height = Relative(1/2), ticks = (ticks_val, ticks_str) )
    fig
    

    It should give you this:

    logarithmic field