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

**URL:** https://discourse.julialang.org/t/can-i-specify-color-of-each-rectangle-in-makie-jls-heatmap/95314
**Category:** Visualization
**Tags:** makie
**Created:** [February 28, 2023, 7:18am UTC](https://discourse.julialang.org/t/can-i-specify-color-of-each-rectangle-in-makie-jls-heatmap/95314 "2023-02-28T07:18:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)
#### Post date: [February 28, 2023, 7:18am UTC](https://discourse.julialang.org/t/can-i-specify-color-of-each-rectangle-in-makie-jls-heatmap/95314/1 "2023-02-28T07:18:32Z")

</div>

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.

```julia
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.  
Tomas

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [February 28, 2023, 9:54am UTC](https://discourse.julialang.org/t/can-i-specify-color-of-each-rectangle-in-makie-jls-heatmap/95314/2 "2023-02-28T09:54:07Z")

</div>

Check this:

```julia
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])

```

 ![Makie_meshscatter_heatmap_colored](https://global.discourse-cdn.com/julialang/original/3X/1/7/1729a9de99d077768afbef88293d7922c803a14b.png)

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [February 28, 2023, 10:34am UTC](https://discourse.julialang.org/t/can-i-specify-color-of-each-rectangle-in-makie-jls-heatmap/95314/3 "2023-02-28T10:34:23Z")

</div>

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

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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/d/0dd58c6479d75cf0ab1a97ab66c8a9ea4d98937e.png)

---

<div class="post-metadata">

### Author: ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)
#### Post date: [February 28, 2023, 11:44am UTC](https://discourse.julialang.org/t/can-i-specify-color-of-each-rectangle-in-makie-jls-heatmap/95314/4 "2023-02-28T11:44:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [March 1, 2023, 10:33am UTC](https://discourse.julialang.org/t/can-i-specify-color-of-each-rectangle-in-makie-jls-heatmap/95314/5 "2023-03-01T10:33:22Z")

</div>

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:

 ![Makie_meshscatter_heatmap_colored2](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae1561d6b8b83aa20365010d9efb6a2513422ddc.png)
