Can `Plots.jl` `heatmap` coordinates start at 1 instead of 0.5?

For example, I want to heatmap the following matrix:

julia> m = [
         1   1  -1  -1
        -1  -1  -1   1
         1   1   1   1
        -1  -1   1   1
       ]

using Plots

heatmap(m)

But if I set xlims and ylims, the image will be be clipped from 1 to 4, whereas the original plot starts from 0.5 to 4.5. I know in Python you can set extent to avoid this.

heatmap(m; xlims=extrema(axes(m, 1)), ylims=extrema(axes(m, 2)))

Is there a Plots.jl equivalent when I am not using PyPlot?

Maybe heatmap(1.5:4.5, 1.5:4.5, m) (although this is equivalent to nodal extents, not cells centers) ?

Please also see the discussions in [BUG] image tick positions misplaced when specifying image axes · Issue #4158 · JuliaPlots/Plots.jl · GitHub.

1 Like

Thanks, I tried the following code and it works:

heatmap(m; xlims=extrema(axes(m, 1)) .+ (-0.5, 0.5), ylims=extrema(axes(m, 2)) .+ (-0.5, 0.5))

1 Like

This question is a bit more general and both limits, [0.5 4.5] or [1 4] are correct. It all depends on what we want. In GMT this has been called the grid vs pixel registration and in Geotiff it’s known by the Pixel-is-area vs Pixel-is-point. Next fig and the GMT link above describe more these two ways of storing grids and images coordinates.

In GMT.jl to get the [1 4] limits (that is, to use the grid registration) you’d do

imshow(mat2grid(m))

and

imshow(mat2grid(m, reg=1))

to get the [0.5 4.5]

1 Like

I agree that we should have a higher level abstraction in Plots for this.
A PR would be very much welcome.