How to plot on a square grid with specific colours?

I think you should be able to manually set the colormap and aspect ratio:

using Plots

Z = rand(0:2, (10, 10));

heatmap(Z; color=palette(:Blues, 3), aspect_ratio=1, size=(600, 600))

I was a bit curious about if we could tweak the colorbar ticks, but it looks like that is not directly supported just yet ([FR] Colorbar properties · Issue #3174 · JuliaPlots/Plots.jl · GitHub). I only did a quick search though, so maybe there is another way.

Here is a similar example in Makie.jl if that might be useful:

f = Figure()
ax = Axis(f[1, 1])
hm = heatmap!(ax, Z'; colormap=cgrad(:Blues, 3; categorical=true))
Colorbar(f[1, 2], hm; ticks=0:2)
colsize!(f.layout, 1, Aspect(1, 1.0))
resize_to_layout!(f)
f

which has deep support for further customizations (e.g., Colorbar, ticks, and aspect ratio)

1 Like