Can I specify color of each rectangle in Makie.jl's heatmap?

Hi all,

I would like to plot a 2d map in Makie, where I can specify exactly a color in each rectangle. Something like a heatmap, where the color would be prescribed and not taken from a colormap. I have tried to fudge the heatmap, but the results looks weird. Let me provide a simple example of what I have on mind.

using GLMakie

x = [1, 1, 1, 2, 2, 2, 3, 3, 3];
y = [1, 2, 3, 1, 2, 3, 1, 2, 3];
z = [:black, :teal, :brown :gold, :wheat, :green, :red, :blue, :yellow]
heatplot(x, y, z)

Thanks for help in advance.

Check this:

using Makie, GLMakie, GeometryBasics

x = [1, 1, 1, 2, 2, 2, 3, 3, 3];
y = [1, 2, 3, 1, 2, 3, 1, 2, 3];
z = [:black, :teal, :brown, :gold, :wheat, :green, :red, :blue, :yellow]

f = Figure()
axes = Axis(f[1, 1])
p = Polygon([Point2f(0,0), Point2f(1,0), Point2f(1,1), Point2f(0,1)])
sq = GeometryBasics.mesh(p)
meshscatter!(axes, x, y, marker=sq, markersize=1, color=z, shading=false)
xlims!(axes, [1, 4])
ylims!(axes, [1, 4])

There might be no overload for a vector of colors, but there’s one for a matrix of colors

x = 1:3
y = 1:3
z = Makie.to_color([:black  :teal  :brown; :gold  :wheat  :green; :red  :blue  :yellow])
heatmap(x, y, z)

1 Like

Thank you both guys. I know the community will not betray me.

As an expression of gratitude, yesterday, I showed some plots to the die hard matplotlib / tikz guys and they were impressed by quality of Makie plots.


The accepted solution does not use the list of input points as in OP.

One of the advantages of using meshscatter() with an arbitrary list of input points is that the squares do not need to match a matrix grid, and can be anywhere. Furthermore, we can control the size of the squares. An example with an extra pink square:

1 Like