# Heatmap: Assign colors to discrete values

Hi,

I want to plot a 2D array with heatmap. The array consists of 1s, 2s and 3s. Now I want to assign each value a color, e.g. 1: black, 2: green, 3: blue.

I just can’t figure out how I can do that.

Something like this?

``````julia> using Plots
julia> m=5
julia> n=5
julia> array = rand(1:3,m,n)
5×5 Array{Int64,2}:
3  1  2  1  1
2  2  3  2  3
2  1  1  1  3
2  1  2  1  2
2  3  3  2  3
``````

With `m=100`, `n=100`, I get:

1 Like

Nope, sorry. I meant something like

If I have the Matrix

0 1 2
2 1 0
1 0 1

I just want an image out of it. So I have a 3x3 grid with the cells having those values and those colors.
I think images.jl does something like that but I’m still trying to figure out how it works.

Hm. Your description is not very clear to me. In the first question, you referred to a matrix of 1s, 2s, and 3s, where you associated certain colors to each number.

This time you use numbers 0, 1 and 2, instead of 1s, 2s and 3s, and you suggest a 3x3 matrix.

Anyway, you also refer to an image. So:

``````julia> im = rand([colorant"red", colorant"green", colorant"blue"],100,100)
julia> plot(im)
``````

[Of course, the only reason why I use `rand` is because I have no clue as to what matrix layout you want.]

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

while:

``````plot(num2col.(mat))
``````

gives:

2 Likes

Let me clarify:

0: white
1: green
2: red
3: black

Now, if I have the matrix

``````0 3
2 1
``````

I’d like to see

The problem with plots() is that it introduces an orientation of my data. It flips and/or rotates my data since the axis to have a direction. I just want an image.

I think images.jl can do that but I didn’t figure it out in the last few hours.

That looks interesting. How would I generate a image, like .png or .jpg out of this?

Isn’t the confusion here only because you used `contour` here, instead of `heatmap`? This is what you want (replacing that line in @BLI answer):

``````heatmap(array,fill=true,lw=0,c=colGRAD)
``````

2 Likes
``````savefig("./file.png")
``````

I wasn’t able to try that out. What exactly is colGRAD? Furthermore: The axis have an direction, they grow which probably again does introduce an orientation of my data and thus not exactly what I want. Otherwise, the axis would be useless. Thanks

`colGRAD` is just a utility variable to store a color gradient set up by the colors in the argument of the `cgrad` function.

``````heatmap(array,fill=true,lw=0,c=colGRAD,
framestyle=:box,xticks=nothing,yticks=nothing,legend=false,
size=(400,400))
savefig("file.png")
``````

I’m very confused by Julias docs. What type is colGrad or rather what type does the argument c take? SO I know what kind of “object” I can pass there.

This is run directly in a Jupyter notebook:

1 Like

To find the type, simply do:

``````typeof(colGRAD)
``````

Yeah - had a dependency issue on my system I didn’t catch. I’m also very new to Jupyter and probably did some stupid thing.

Got it working, thanks.

I’m using the `Plots.jl` package (usually). The documentation is somewhat difficult to figure out, but when you work through it and do some examples, it is possible to figure out most (?) possibilities: `http://docs.juliaplots.org/latest/`.

I wished there were a thing like the C++ Reference.

With dependencies I meant ImageMagick Package + local installation of it. Somehow it needs that.

Well, I don’t think it is fair to complain about the documentation. Remember that these are free tools; the contributors provide code which is often developed in their free time.

1 Like

Of course and I appreciate everyone spending time on something like that. I try myself to report bugs and write stuff if possible.