# How to plot on a square grid with specific colours?

**URL:** <https://discourse.julialang.org/t/how-to-plot-on-a-square-grid-with-specific-colours/77801>\
**Category:** Visualization\
**Created:** [March 12, 2022, 5:30pm UTC](https://discourse.julialang.org/t/how-to-plot-on-a-square-grid-with-specific-colours/77801 "2022-03-12T17:30:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [March 12, 2022, 5:30pm UTC](https://discourse.julialang.org/t/how-to-plot-on-a-square-grid-with-specific-colours/77801/1 "2022-03-12T17:30:42Z")

</div>

I am trying to plot on a 2D grid the presence of “agents” (integers) on a certain plot, but I can"t get:

- how to associate a specific colour to a certain cell integer
- how to force the plot to be squared

Here it is what I have achieved so far:

```julia
using Random, Plots
n = 16
nRed = Int(round(n*n*0.1))
nBlue = Int(round(n*n*0.2))
cells = fill(0,n*n)
#cells = Array{Union{Missing,Int64}}(missing,n,n)
cells[1:nRed] .= 1
cells[nRed+1:nRed+nBlue] .= 2
shuffle!(cells)
heatmap(reshape(cells,n,n), legend=nothing, title="Interation 1")

```

Producing:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/1/e13d908e9c4d06fefa7659c6ce755ad69a30c6e4.png)

---

<div class="post-metadata">

**Author:** ![icweaver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/icweaver/32/45764_2.png) [@icweaver](https://discourse.julialang.org/u/icweaver)\
**Post date:** [March 13, 2022, 4:39am UTC](https://discourse.julialang.org/t/how-to-plot-on-a-square-grid-with-specific-colours/77801/2 "2022-03-13T04:39:01Z")

</div>

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

```julia
using Plots

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

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

```

 ![plots](https://global.discourse-cdn.com/julialang/original/3X/d/a/da02a08bc1a65fe80db1fc5374f1f3abd730caa0.png)

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](https://github.com/JuliaPlots/Plots.jl/issues/3174)). 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:

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

```

 ![makie](https://global.discourse-cdn.com/julialang/original/3X/5/9/598749c05712b5b0610d3159677f27547dd7bae7.png)

which has deep support for further customizations (e.g., [Colorbar](https://makie.juliaplots.org/stable/examples/layoutables/colorbar/index.html), [ticks](https://makie.juliaplots.org/stable/examples/layoutables/axis/index.html#modifying_ticks), and [aspect ratio](https://makie.juliaplots.org/stable/tutorials/aspect-tutorial/index.html))

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [March 13, 2022, 6:41am UTC](https://discourse.julialang.org/t/how-to-plot-on-a-square-grid-with-specific-colours/77801/3 "2022-03-13T06:41:29Z")

</div>

Thanks, I am almost there. Only, the aspect\_ratio seems to relate to the inner part of the figure, not the whole figure like in your screenshot…

```julia
using Random, Plots
n = 16
nRed = Int(round(n*n*0.1))
nBlue = Int(round(n*n*0.2))
cells = fill(0,n*n)
cells[1:nRed] .= 1
cells[nRed+1:nRed+nBlue] .= 2
shuffle!(cells)
mypal = [:white,:red,:blue]
# or mypal = [RGB(255/255, 255/255, 255/255), RGB(255/255, 0/255, 0/255), RGB(0/255, 0/255, 255/255)]
heatmap(reshape(cells,n,n), legend=nothing, title="Iteration 1", color=mypal,aspect_ratio=1)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/844dc77b40d4c7f1fe0e6f7422f3ca52779ead71.png)

[EDIT]: Ok, I didn’t specified the size, while specifying the size I also obtain the squared figure, thanks!
