Using tickformat in a Makie.jl colorbar

31 views Asked by At

My plot looks like this:

enter image description here

How can I change the colorbar to show simple numbers? I've tried tickformat but can't get the syntax right. Can anyone help?

Mke.Colorbar(fig[1, 2], limits=extrema(longt.rat), colormap="hawaii", scale=log10, tickformat="%.0f", label="Multiple of national average\nfunding per capita")

For example, the above simply labels each tick with the literal "%.0f".

Thanks!

Edit: Here is an MWE

using GeoStats, GeoIO, CSV, DataFrames, Colors, ColorSchemes
import CairoMakie as Mke

# List of London Boroughs
lon = ["Westminster", "Ealing", "Kingston upon Thames", "Islington", "Merton", "Hackney", "Camden", "Wandsworth", "Enfield", "Southwark", "City of London", "Lambeth", "Hillingdon", "Tower Hamlets", "Kensington and Chelsea", "Greenwich", "Haringey", "Richmond upon Thames", "Hounslow", "Harrow", "Lewisham", "Hammersmith and Fulham", "Newham", "Waltham Forest", "Croydon", "Brent", "Redbridge", "Barnet", "Bromley", "Barking and Dagenham", "Havering", "Sutton", "Bexley"]

# Shape file from ONS: https://geoportal.statistics.gov.uk/datasets/127c4bda06314409a1fa0df505f510e6_0/explore
print("Reading shapefile         ... ")
@time LAs = GeoIO.load("LAD_DEC_2023_UK_BFC.shp") |> Rename("LAD23CD" => "LADcode")

#print("Reading combo.csv         ... ")
#@time combo = CSV.read("combo.csv", DataFrame)

# Subset of CSV data just covering London
combo = DataFrame(
    LADcode=["E09000033", "E09000009", "E09000021", "E09000019", "E09000024", "E09000012", "E09000007", "E09000032", "E09000010", "E09000028", "E09000001", "E09000022", "E09000017", "E09000030", "E09000020", "E09000011", "E09000014", "E09000027", "E09000018", "E09000015", "E09000023", "E09000013", "E09000025", "E09000031", "E09000008", "E09000005", "E09000026", "E09000003", "E09000006", "E09000002", "E09000016", "E09000029", "E09000004"],
    newLocalAuthority=["Westminster", "Ealing", "Kingston upon Thames", "Islington", "Merton", "Hackney", "Camden", "Wandsworth", "Enfield", "Southwark", "City of London", "Lambeth", "Hillingdon", "Tower Hamlets", "Kensington and Chelsea", "Greenwich", "Haringey", "Richmond upon Thames", "Hounslow", "Harrow", "Lewisham", "Hammersmith and Fulham", "Newham", "Waltham Forest", "Croydon", "Brent", "Redbridge", "Barnet", "Bromley", "Barking and Dagenham", "Havering", "Sutton", "Bexley"],
    Total_Awarded=[1551248258, 110166558, 38033540, 865003501, 46621989, 395602425, 915273792, 197296888, 86772623, 573171051, 321575633, 665770100, 39514648, 374049415, 307431197, 812345843, 181261693, 176262413, 131028168, 48236040, 173721339, 257659057, 246970981, 95473179, 86525712, 304128061, 77681457, 82934564, 88862200, 63604751, 46974062, 42463929, 41183878],
    subpop=[205087, 366127, 167845, 216767, 215324, 259956, 210390, 328367, 329601, 306374, 8618, 317498, 304792, 312273, 143940, 289254, 264130, 195232, 287940, 260987, 299810, 183295, 350626, 278050, 390506, 338918, 309836, 388639, 329830, 218534, 262022, 209517, 246543]
    )

print("Processing data           ... ")
@time begin
# Funding per capita
    natave = sum(skipmissing(combo.Total_Awarded)) / sum(skipmissing(combo.subpop))
    combo.rat .= (combo.Total_Awarded) ./ (combo.subpop) ./ natave
    col = log10.(combo.rat)
    mini = minimum(col)
    maxi = maximum(col)
end

print("Joining combo to geometry ... ")
@time begin
# Add combo to the GeoTable
    table1 = values(LAs) |> DataFrame
    table1.index = [i for i in 1:nrow(table1)]
    leftjoin!(table1, combo, on=:LADcode, matchmissing=:equal)
    sort!(table1, :index)
    gt = georef(table1, LAs.geometry)
end

# Draw a map with colorbar
function citymap(gt, LA_List, title)
    longt = gt |> Filter(row -> !ismissing(row.newLocalAuthority) && row.newLocalAuthority in LA_List)
    col = log10.(longt.rat) # Can't currently have consistent colours across different plots
    fig, ax, v = viz(longt.geometry, color=col, colorscheme=:hawaii, colorrange=extrema(col), facetcolor="black", showfacets=true)
    Mke.Colorbar(fig[1, 2], limits=extrema(longt.rat), colormap="hawaii", scale=log10, #=tickformat="%.0f",=# label="Multiple of national average\nfunding per capita")
    Mke.Label(fig[0, :], title, fontsize=30)
    ax.aspect = Mke.DataAspect()
    Mke.hidedecorations!(ax)
    Mke.display(fig)
end
@time citymap(gt, lon, "Greater London")
1

There are 1 answers

0
Tim Gebbels On

@Dan Getz said: Try the following tickformat format: tickformat="{:.3f}". This works for me so I'm marking as a solution. Thank you!