0
$\begingroup$

After looking through these forums for answers that could work for my problem, I could not find anything. I want to make a heat map comparing values that are in a nested list. That is, I have a nested list of size n x n, and each position in the list is a rational number. How could I make a heat map comparing the greater/lesser of these numbers. The higher numbers would be one color and the lower would be another.

An example first three lines of a list of Length 9 is:

{{0., 2.03181, 10.9162, 9.81852, 19.9333, 9.74689, 0.826292, 1.61575, 
  6.11642}, {2.52762, 0., 12.3072, 13.0194, 5.9971, 20.1544, 2.1197, 
  0.611078, 5.01625}, {17.5098, 6.42256, 0., 5.32809, 7.09947, 
  22.5269, 5.43391, 5.56034, 0.48698}}

All assistance is appreciated.

Best,

CK

$\endgroup$
1
  • $\begingroup$ Would ArrayPlot@yourList work? $\endgroup$ Commented Feb 14, 2013 at 4:14

1 Answer 1

8
$\begingroup$

This is in the docs* and might be what you want:

(* the catch is you have to know that this is what you need to be able to find it in the docs -- it is not necessarily intuitive for a new user to go to array plot for this solution)

enter image description here

or you can blend between two colours:

ArrayPlot[RandomReal[1, {10, 20}], ColorFunction -> (Blend[{Yellow, Purple}, #1] &)]

enter image description here

or make a binary choice:

ArrayPlot[{{0., 2.03181, 10.9162, 9.81852, 19.9333, 9.74689, 0.826292,
    1.61575, 6.11642}, {2.52762, 0., 12.3072, 13.0194, 5.9971, 
   20.1544, 2.1197, 0.611078, 5.01625}, {17.5098, 6.42256, 0., 
   5.32809, 7.09947, 22.5269, 5.43391, 5.56034, 0.48698}}, 
 ColorFunction -> (If[#1 > 10, Purple, Yellow] &), 
 ColorFunctionScaling -> False]

enter image description here

ColorFunctionScaling is an option that by default is True and means that the values in your list are scaled. There may be occasions where you want to apply your own scaling function. You would do this as a pure function for ColorFunction.

Here is default scaling using a "wimbledon" gradient (green-purple)

ArrayPlot[{{0., 2.03181, 10.9162, 9.81852, 19.9333, 9.74689, 0.826292,
    1.61575, 6.11642}, {2.52762, 0., 12.3072, 13.0194, 5.9971, 
   20.1544, 2.1197, 0.611078, 5.01625}, {17.5098, 6.42256, 0., 
   5.32809, 7.09947, 22.5269, 5.43391, 5.56034, 0.48698}}, 
 ColorFunction -> (Blend[{Green, Purple}, #1] &), 
 ColorFunctionScaling -> True]

enter image description here

Here is a new plot, this time scaling your numbers between 0 and 40.

ArrayPlot[{{0., 2.03181, 10.9162, 9.81852, 19.9333, 9.74689, 0.826292,
    1.61575, 6.11642}, {2.52762, 0., 12.3072, 13.0194, 5.9971, 
   20.1544, 2.1197, 0.611078, 5.01625}, {17.5098, 6.42256, 0., 
   5.32809, 7.09947, 22.5269, 5.43391, 5.56034, 0.48698}}, 
 ColorFunction -> (Blend[{Green, Purple}, Rescale[#1, {0, 40}]] &), 
 ColorFunctionScaling -> False]

enter image description here

$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.