# Symbol to color in heatmap plot

**URL:** <https://discourse.julialang.org/t/symbol-to-color-in-heatmap-plot/78839>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [March 31, 2022, 10:47pm UTC](https://discourse.julialang.org/t/symbol-to-color-in-heatmap-plot/78839 "2022-03-31T22:47:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsaravia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lsaravia/32/33532_2.png) [@lsaravia](https://discourse.julialang.org/u/lsaravia)\
**Post date:** [March 31, 2022, 10:47pm UTC](https://discourse.julialang.org/t/symbol-to-color-in-heatmap-plot/78839/1 "2022-03-31T22:47:18Z")

</div>

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?

```julia

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

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

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

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [April 1, 2022, 3:48pm UTC](https://discourse.julialang.org/t/symbol-to-color-in-heatmap-plot/78839/2 "2022-04-01T15:48:48Z")

</div>

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

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

```
