Heatmap: Assign colors to discrete values

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

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.

Thanks :slight_smile:

That only works for your approach with heatmap right?

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

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

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

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. :confused: so I can’t use savefig()

Or I don’t understand you.

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

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 :

1 Like

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”

1 Like

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

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

2 Likes

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. :slight_smile: I think it boils down to: How to save an matrix of RGB values as an image.

Thanks a lot for your help.

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> 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> 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])

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

thanks a lot for the help!