Symbol to color in heatmap plot

I have a Symbol matrix that could have 3 Symbols say :Ø,:F, :V but sometimes have not all the symbols how to plot it with the same colors?


m = fill(:Ø, 10, 10)
heatmap(m,aspect_ratio=1,legend=:none,xticks=:none,
yticks=:none,framestyle=:none,color=[  :black, :red, :green   ])

if I add other Symbol

m[:,2] .= :V
heatmap(m,aspect_ratio=1,legend=:none,xticks=:none,
yticks=:none,framestyle=:none,color=[  :black, :red, :green   ])

Colors change, and with another one colors change again

m[:,1] .= :F
heatmap(m,aspect_ratio=1,legend=:none,xticks=:none,
yticks=:none,framestyle=:none,color=[  :black, :red, :green   ])

how do I map Symbol to colors?

This is a fun question. Things get a little bit more clearly, when you turn on the colorbar via colorbar = true.

You will then notice that the range the colors span is different in each plot because there are a different amount of values. You can fix the range of values you expect via clims = (0.5, 2.5).

Why these values? It seems the symbols get mapped to the values of the midpoints where they appear first, so in the first plot all “O”'s have the value 0.5, as well as in the second and V will have the value 1.5. But in the third case O is not the first anymore, so now F gets 0.5 and V stays at 1.5 while O gets 2.5.

So clearly its best to convert your symbols to values beforehand and then plot these.
Something like

m = [ s == :O ? 1 : s == :V ?  2 : s == :F ? 3 : NaN for s in data]
reshape!(m, 10, 10)
heatmap(m,aspect_ratio=1,legend=:none,xticks=:none,
yticks=:none,framestyle=:none,color=[  :black, :red, :green   ], clims = (1,3) )  
1 Like