# Heatmap: Assign colors to discrete values

**URL:** <https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739>\
**Category:** New to Julia\
**Created:** [October 4, 2020, 3:24pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739 "2020-10-04T15:24:48Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![pascal-mueller](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@pascal-mueller](https://discourse.julialang.org/u/pascal-mueller)\
**Post date:** [October 4, 2020, 6:00pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/23 "2020-10-04T18:00:31Z")

</div>

So you approach of replacing the elements of the matrix with color values works nice but how can I now save it? I first thought I got it with svefig() but that doesn’t seem to work. So what I run is

```julia
mat = [0 1 2;2 0 3;1 3 1]
#
function num2col(i)
    if i == 0
        return colorant"white"
    elseif i == 1
        return colorant"green"
    elseif i == 2
        return colorant"red"
    elseif i == 3
        return colorant"black"
    end
end
#
num2col.(mat)

```

which works nice in Jupyter but I can’t figure out how to actually turn it into a file.

---

<div class="post-metadata">

**Author:** ![pascal-mueller](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@pascal-mueller](https://discourse.julialang.org/u/pascal-mueller)\
**Post date:** [October 4, 2020, 6:08pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/24 "2020-10-04T18:08:39Z")

</div>

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![pascal-mueller](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@pascal-mueller](https://discourse.julialang.org/u/pascal-mueller)\
**Post date:** [October 4, 2020, 6:09pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/25 "2020-10-04T18:09:20Z")

</div>

That only works for your approach with heatmap right?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 4, 2020, 6:15pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/26 "2020-10-04T18:15:03Z")

</div>

It saves any plot. The other alternative does not create a plot, just prints out the color matrix iteratively.

---

<div class="post-metadata">

**Author:** ![pascal-mueller](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@pascal-mueller](https://discourse.julialang.org/u/pascal-mueller)\
**Post date:** [October 4, 2020, 6:16pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/28 "2020-10-04T18:16:44Z")

</div>

Exactly. 🙂 Any way I can save that matrix as a png file?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 4, 2020, 6:17pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/29 "2020-10-04T18:17:59Z")

</div>

Probably there are many ways to do that, plotting it is one of those.

---

<div class="post-metadata">

**Author:** ![pascal-mueller](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@pascal-mueller](https://discourse.julialang.org/u/pascal-mueller)\
**Post date:** [October 4, 2020, 6:21pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/31 "2020-10-04T18:21:07Z")

</div>

I can’t plot it. Plotting introduces oriented and ordered axes which destroy the orientation. You can see that above. So your way of creating a heatmap doesn’t work. 😕 so I can’t use savefig()

Or I don’t understand you.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 4, 2020, 6:26pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/32 "2020-10-04T18:26:05Z")

</div>

Something like this (`mat` is the number matrix, `M` is the color matrix generated with the `num2col` function above:

```julia
julia> mat = [0 1 2;2 0 3;1 3 1]
julia> M = num2col.(mat);
julia> heatmap(mat,c=M,xaxis=nothing,yaxis=nothing,legend=nothing,size=(400,400))

```

Produces

![file](https://global.discourse-cdn.com/julialang/original/3X/b/0/b038d79c8a4b007a1bdf03e6ddb3109750134310.png) :

---

<div class="post-metadata">

**Author:** ![pascal-mueller](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@pascal-mueller](https://discourse.julialang.org/u/pascal-mueller)\
**Post date:** [October 4, 2020, 6:33pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/33 "2020-10-04T18:33:57Z")

</div>

> [@lmiq](#):
>
> ```julia
> julia> mat = [0 1 2;2 0 3;1 3 1]
> julia> M = num2col.(mat);
> julia> heatmap(mat,c=M,xaxis=nothing,yaxis=nothing,legend=nothing,size=(400,400))
> 
> ```

Exacly, and that’s wrong. The heatmap, even if you hide the axis labels, orders the data. Look at the first row in the data: 0 1 2, now 0 is white. So the entry (1,1,1) shouldb e white i.e. the top left corner should be white but in your plot it’s red. That’s what I mean with “it orders the data” or “orientates the plot”

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 4, 2020, 7:39pm UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/35 "2020-10-04T19:39:22Z")

</div>

Actually what I think is missing is adding `yflip=true` to the heatmap:

```julia
julia> mat
3×3 Array{Int64,2}:
 0 1 2
 2 1 0
 1 0 1

julia> colors = num2col.(mat);

julia> heatmap(mat,c=colors,yflip=true)

julia> savefig("file.png")

```

![file](https://global.discourse-cdn.com/julialang/original/3X/5/9/59b36291b65745d7edd24fd897c211a8787a58d1.png)

---

<div class="post-metadata">

**Author:** ![pascal-mueller](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@pascal-mueller](https://discourse.julialang.org/u/pascal-mueller)\
**Post date:** [October 5, 2020, 7:37am UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/36 "2020-10-05T07:37:39Z")

</div>

That might work for this specific data. Flip the matrix and you end up with the same problem, no? The problem is, that heatmap is a visualization tool for data on a cartesian coordinate system. I basically have a graph. I don’t have a coordinate system at all. 🙂 I think it boils down to: How to save an matrix of RGB values as an image.

Thanks a lot for your help.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 5, 2020, 8:43am UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/37 "2020-10-05T08:43:40Z")

</div>

> [@pascal-mueller](#):
>
> Flip the matrix and you end up with the same problem, no?

Uhm… not sure what you mean here. If you flip the matrix you would get a different matrix, which could be printed with this as well.

Perhaps you can explain more precisely what is your problem, so someone can help you specifically. We might have a case here where the simplification of your description is not representing what you really want to achieve.

Also, this is an option, to save it to a PDF file (other formats are available, but the pixels  
get blurred (there is probably some option to just print a 3x3 pixel image, but I am not  
sure if this is what you want, as using it later will not be very practical).

```julia
julia> using Images, ImageMagick

julia> function num2col(i)
           if i == 0
               return Images.colorant"red"
           elseif i == 1
               return Images.colorant"green"
           else
               return Images.colorant"blue"
           end
       end
num2col (generic function with 1 method)

julia> mat = [0 1 2;2 0 1;1 3 1]
3×3 Array{Int64,2}:
 0 1 2
 2 0 1
 1 3 1

julia> c = num2col.(mat)
3×3 Array{RGB{N0f8},2} with eltype RGB{Normed{UInt8,8}}:
 RGB{N0f8}(1.0,0.0,0.0) RGB{N0f8}(0.0,0.502,0.0) RGB{N0f8}(0.0,0.0,1.0)
 RGB{N0f8}(0.0,0.0,1.0) RGB{N0f8}(1.0,0.0,0.0) RGB{N0f8}(0.0,0.502,0.0)
 RGB{N0f8}(0.0,0.502,0.0) RGB{N0f8}(0.0,0.0,1.0) RGB{N0f8}(0.0,0.502,0.0)

julia> save("test.pdf",c)

```

Edit: if a gray scale is ok, you can use the more compact:

```julia
julia> using Colors, ImageMagick, Images

julia> m = rand(1:10,10,10);

julia> img = Gray.((m.-minimum(m))/maximum(m)) # image appears in notebook here

julia> save("test.pdf",img)

```

(where the minimum and maximum stuff is because the color scales are mapped from numbers in [0,1])

---

<div class="post-metadata">

**Author:** ![pascal-mueller](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@pascal-mueller](https://discourse.julialang.org/u/pascal-mueller)\
**Post date:** [October 5, 2020, 9:21am UTC](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739/38 "2020-10-05T09:21:30Z")

</div>

Yeah so BLI has a good example a few answers up on what I wanted to do. The post with the two images. I’m done though, I got it working. See [Gofile - Free file sharing and storage platform](https://gofile.io/d/2zf6V5)

thanks a lot for the help!

[Previous page](https://discourse.julialang.org/t/heatmap-assign-colors-to-discrete-values/47739.md?page=1)
