Scale Division in Makie

Hi, I was wondering if there is a way to put a scale division in a heatmap plot using Makie. See the following plot as a reference, where I would like to implement both the line and the value in the corner that is positioned center-right:

If possible I would like to couple it to a specific pixel ratio, so let’s say I know each pixel has a “real” width Δx. I want to make sure that for different plots with different Δx, that the scale division takes up a certain amount of pixels C (constant for each plot), but calculates the corresponding value (C*Δx) and displays it above the scale division.

Thanks in advance!

One option would be to calculate the data distance covered by some pixel width by listening to ax.finallimits and ax.scene.px_area. Then you can calculate the line and text position in pixel space for the axis scene, and update accordingly. This only works when there’s no axis scale applied.

using DelimitedFiles


volcano = readdlm(Makie.assetpath("volcano.csv"), ',', Float64)


f = Figure()
ax = Axis(f[1, 1])
co = contourf!(volcano, levels = 10, colormap = :Blues)

relpoints = Observable(Point2f[(0, 0), (1, 1)])
label = Observable("label")
width_px = Observable(50.0)
offset_px = Observable(20.0)

linesegments!(ax, relpoints, space = :pixel, color = :black, linewidth = 5)
text!(ax, @lift(($relpoints[1] + $relpoints[2]) * 0.5), space = :pixel, align = (:center, :bottom), text = label, offset = (0, 5))

onany(ax.finallimits, ax.scene.px_area, width_px, offset_px) do lims, pxa, width_px, offset_px
    xmin, xmax = extrema(lims)[1]
    br = Makie.bottomright(pxa) - pxa.origin
    width_data = width_px / widths(pxa)[1] * widths(lims)[1]
    label[] = "$(round(width_data, sigdigits = 3)) km"

    relpoints[] = [
        br + Point2f(-width_px - offset_px, offset_px),
        br + Point2f(-offset_px, offset_px),
    ]
end

notify(ax.finallimits)

f

1 Like