Color Maps
From ComputingForScientists
Contents 
1. Color Maps
1.1. Objective
 To introduce colormaps.
1.2. Motivation
 To display a table of numbers (i.e., a matrix) as a color image, you need to define a colormap.
1.3. Introduction

1.4. Motivation

1.5. Motivating Question
Suppose that you have a table of numbers. How would you represent these numbers as colors?
1.6. Answer
 Spreadsheet approach: [1]
 For each numeric value, assign a color (Similar to painting by numbers).
 One can also assign colors for ranges (e.g., blue = 6.66 to 10.00).
 Keep in mind that we are assigning numbers to colors and that colors are associated with a set of numbers (for example, red is 255, 0, 0 or 1.0, 0.0, 0.0).
The table of numbers in this image is colored based on the rules specified in the bottom image.
1.7. Colormap definition
A colormap maps (translates) a decimal number (or a range of numbers) to a color. Humans refer to the colors with words. MATLAB and other programs generally understand both these words or a list of three numbers that is equivalent to these words.
Spreadsheet colormap:

Giraffe colormap

1.8. How to create a colormap
 To create a colormap, we need to create a matrix that tells MATLAB the list of allowed colors (a colormap) (using the r,g,b representation).
 That is, to render a matrix containing decimal numbers as an image, we need another "helper" matrix, called the colormap matrix (which always has three columns).
 When asked to display a matrix as an image, MATLAB will refer to this colormap.
 It will associate the lowest value in the matrix the color given in the first row of the colormap.
 It will associate the highest value in the matrix the color given in the last row of the colormap.
To tell MATLAB that the allowed colors that it may use when it is asked to plot a table of numbers, use these commands.
C = [1,0,0;0,1,0;0,0,1]; colormap(C)
 The first command creates a colormap matrix, which has three columns and as many rows as we want. In the above example, the first row corresponds to red, the second row corresponds to blue, etc.
 The second command tells MATLAB: "These are the colors that you are allowed to use".
Now we are ready to plot a different matrix as an image. The matrix can values that are any decimal numbers  MATLAB assumes that the lowest value in the matrix should be associated with color in the first row in the colormap.
1.9. Important
 In MATLAB, you render a matrix as an image by defining a colormap.
 But the colormap is also a matrix! This fact often causes confusion.
 Think of the colormap matrix as the "helper" matrix. The colormap always has three columns. In contrast, the matrix to be rendered can have any number of columns.
 To minimize confusion, I'll always use the letter
C
to refer to the colormap helper matrix and another letter as the matrix that we want to render as an image.
1.10. The imagesc
function
To display a matrix M
as an image, use the command imagesc(M)
Example:
% Specify the allowed colors C = [1.0, 0.0, 0.0 ; 0.0, 1.0, 0.0 ; 0.0, 0.0, 1.0]; % Tell MATLAB to use these colors colormap(C); % Create a matrix with 9 rows and 10 columns % having values between zero and 10. Each time the % program is executed, M will have different values! M = round(rand(10*rand(9,10))) % Tell MATLAB to render the matrix M as an image imagesc(M); % Tell MATLAB to show a colorbar colorbar; % Save as .eps and .png files print depsc imagscdemo.eps print dpng imagescdemo.png
1.11. The colorbar
function
The colorbar
function tells MATLAB to show a color scale so that one can determine what decimal number in a matrix is associated with each color.
As shown by the colorbar, MATLAB has mapped the numbers
 0.00 to 3.33 to red,
 3.33 to 6.66 to green,
 6.66 to 10.00 to blue,
The green square in the upperleft corner of the image corresponds to the upperleft number in the matrix (5). The blue square in the lowerright corner of the image corresponds to the lowerright number in the matrix (7).
The colorbar has five colors because these are the number of colors (rows) that were specified in the colormap matrix C
. In addition, the color specified in the first row (red) of the colormap matrix was used for the smallest numbers in the matrix M
. The color specified in the last row of the colormap matrix was used for the largest numbers in the matrix M
.
2. Problems
2.1. Interpreting an image I
Which of the following matrices are consistent with the image and colorbar shown? (You may use MATLAB to check your answer.)
Answer 

A. and D. We are looking for a matrix with 9 rows and 10 columns with low numbers on the top and high number near the bottom. WIthout additional information, it is not possible to distinguish between A. and D. 
A. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 
B. 0 10 20 30 40 50 60 70 80 1 11 21 31 41 51 61 71 81 2 12 22 32 42 52 62 72 82 3 13 23 33 43 53 63 73 83 4 14 24 34 44 54 64 74 84 5 15 25 35 45 55 65 75 85 6 16 26 36 46 56 66 76 86 7 17 27 37 47 57 67 77 87 8 18 28 38 48 58 68 78 88 9 19 29 39 49 59 69 79 89 
C. 80 81 82 83 84 85 86 87 88 89 70 71 72 73 74 75 76 77 78 79 60 61 62 63 64 65 66 67 68 69 50 51 52 53 54 55 56 57 58 59 40 41 42 43 44 45 46 47 48 49 30 31 32 33 34 35 36 37 38 39 20 21 22 23 24 25 26 27 28 29 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 9 
D. 9 8 7 6 5 4 3 2 1 0 19 18 17 16 15 14 13 12 11 10 29 28 27 26 25 24 23 22 21 20 39 38 37 36 35 34 33 32 31 30 49 48 47 46 45 44 43 42 41 40 59 58 57 56 55 54 53 52 51 50 69 68 67 66 65 64 63 62 61 60 79 78 77 76 75 74 73 72 71 70 89 88 87 86 85 84 83 82 81 80 
E. 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 
F. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
2.2. Interpreting an image II
Which of the following matrices are consistent with the image and colorbar shown?
Answer 

C. 
A. 90 80 70 60 50 40 30 20 10 89 79 69 59 49 39 29 19 9 88 78 68 58 48 38 28 18 8 87 77 67 57 47 37 27 17 7 86 76 66 56 46 36 26 16 6 85 75 65 55 45 35 25 15 5 84 74 64 54 44 34 24 14 4 83 73 63 53 43 33 23 13 3 82 72 62 52 42 32 22 12 2 81 71 61 51 41 31 21 11 1 
B. 10 9 8 7 6 5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 30 29 28 27 26 25 24 23 22 21 40 39 38 37 36 35 34 33 32 31 50 49 48 47 46 45 44 43 42 41 60 59 58 57 56 55 54 53 52 51 70 69 68 67 66 65 64 63 62 61 80 79 78 77 76 75 74 73 72 71 90 89 88 87 86 85 84 83 82 81 
C. 81 82 83 84 85 86 87 88 89 90 71 72 73 74 75 76 77 78 79 80 61 62 63 64 65 66 67 68 69 70 51 52 53 54 55 56 57 58 59 60 41 42 43 44 45 46 47 48 49 50 31 32 33 34 35 36 37 38 39 40 21 22 23 24 25 26 27 28 29 30 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 
D. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 
E. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 
F. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 
2.3. Experimentation I
Open the MATLAB editor and copy and paste the following commands (You do not need to delete the lines (comments) that start with a %
character. MATLAB will ignore these lines). Save the file to your desktop as cm_experimentI
.
% Specify the allowed colors C = [1.0, 0.0, 0.0 ; 0.0, 1.0, 0.0 ; 0.0, 0.0, 1.0 ; 0.5, 0.5, 0.5 ; 0.0, 0.0, 0.0]; % Tell MATLAB to uses these colors colormap(C); % Create a matrix with two rows M = [1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4]; % Tell MATLAB to render the matrix M as an image imagesc(M); % Tell MATLAB to show a colorbar colorbar;
1. Modify the colormap matrix C
so that when you run the program, the only colors that are shown are red, green, blue and black. Enter your colormap matrix below.
Answer 

C = [1.0, 0.0, 0.0 ; 0.0, 1.0, 0.0 ; 0.0, 0.0, 1.0 ; 0.0, 0.0, 0.0]; colormap(C); M = [1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4]; imagesc(M); colorbar; 
2. Using the same colormap matrix used to answer the previous question, modify the values in the matrix M
so that the middle two columns in the image are blue. Enter your matrix M
below.
Answer 

C = [1.0, 0.0, 0.0 ; 0.0, 1.0, 0.0 ; 0.0, 0.0, 1.0 ; 0.0, 0.0, 0.0]; colormap(C); M = [1 3 3 4; 1 3 3 4; 1 3 3 4; 1 3 3 4]; imagesc(M); colorbar; 
3. What is a colormap matrix, that when used with the matrix M
to answer the previous question, would make the middle two columns red?
Answer 

C = [0.0, 0.0, 1.0 ; 0.0, 1.0, 0.0 ; 1.0, 0.0, 0.0 ; 0.0, 0.0, 0.0]; colormap(C); M = [1 3 3 4; 1 3 3 4; 1 3 3 4; 1 3 3 4]; imagesc(M); colorbar; 
2.4. Experimentation II
Open the MATLAB editor and copy and paste the following commands (You do not need to delete the lines (comments) that start with a %
character. MATLAB will ignore these lines). Save the file to your desktop as cm_experimentII
.
% Specify the allowed colors C = [0.0, 0.0, 0.0; 0.5, 0.5, 0.5 ; 1.0, 1.0, 1.0]; % Tell MATLAB to uses these colors colormap(C); % Create a matrix with two rows M = [0 5 10 0; 0 5 10 0; 0 5 10 0; 0 5 10 0] % Tell MATLAB to render the matrix M as an image imagesc(M); % Tell MATLAB to show a colorbar colorbar;
1. Modify the colormap matrix C
so that when you run the program, the black area is white and the white area are black. Enter your colormap matrix below
Answer 

Note that the lowest values in the matrix were associated with the first color (black  0,0,0) in the colormap matrix C. To replace black with white, change C = [0.0, 0.0, 0.0; 0.5, 0.5, 0.5 ; 1.0, 1.0, 1.0]; to C = [1.0, 1.0, 1.0; 0.5, 0.5, 0.5 ; 0.0, 0.0, 0.0]; or, modify the matrix M so that the elements in the first and last column have the value 10 and the elements in the third column have the value 0. M = [10 5 0 10; 10 5 0 10; 10 5 0 10; 10 5 0 10] 
2. Using the same colormap matrix used to answer the previous question, modify the matrix M
so that so that the gray area is white.
Answer 

M = [10 10 0 10; 10 10 0 10; 10 10 0 10; 10 10 0 10] (could also have changed the 5s in the second column to any number equal to or above 6.67). 
3. Starting with the original program, modify either the colormap matrix C
and/or the matrix M
so that the following image is produced.
Answer 

M = [10 10 10 10; 5 5 5 5; 0 0 0 0; 10 10 10 10] 
4. Starting with the original program, modify either the colormap matrix C
and/or the matrix M
so that the following image is produced.
Answer 

Both % Specify the allowed colors C = [1.0, 1.0, 1.0; 0.5, 0.5, 0.5 ; 0.0, 0.0, 0.0]; % Tell MATLAB to uses these colors colormap(C); % Create a matrix with two rows M = [0 5 10 0; 0 5 10 0; 0 5 10 0; 0 5 10 0] M(2,2) = 0; M(4,3) = 1; % Tell MATLAB to render the matrix M as an image imagesc(M); % Tell MATLAB to show a colorbar colorbar; 
2.5. Create your own colormap
Create a colormap with 8 unique colors. The R,G,B codes for a few colors is listed here: http://cloford.com/resources/colours/500col.htm (you will need to convert them to r,g,b).
Use a nested for
loop to create a matrix with five rows and ten columns with values between 1 and 50 (inclusive).
Plot the matrix.
Answer 

clear; C = [1 1 1;0 0 0;1,0,0;0,1,0;0,0,1;0.5,0.5,0.5;0.1,0.2,0.3;0.3,0.2,0.1]; count = 1; for i = [1:5] for j = [1:10] M(i,j) = count; count = count+1; end end imagesc(M) colormap(C) axis square; colorbar; 
2.6. Black and white image
Use a nested Extra credit: Use a nested 

2.7. Matrix Problem
How should lines clear; M(10,10) = 0; for i = [1:10] for j = [1:10] if (______) % Line A. M(___) = 1; % Line B. else M(i,j) = 0; end end end C = [1 1 1;0 0 0]; imagesc(M); colormap(C); colorbar; 
2.8. Colormap I
Suppose that you have the following colormap matrix: C = [1.0, 0.0, 0.0 ; 0.0, 1.0, 0.0 ; 0.0, 0.0, 1.0]
Which commands could be used to remove the color green and replace it with the color represented by equal amounts of Red, Green, and Blue? Circle the best answer.
A. C(2,1) = 128; C(2,2) = 128; C(2,3) = 128; 
B. C(2,1) = 0.50; C(2,2) = 0.50; C(2,3) = 0.50; 
C. C(1,1) = 0.50; C(2,1) = 0.50; C(3,1) = 0.50; 
D. C(1,1) = 128; C(2,1) = 128; C(3,1) = 128; 
2.9. Colormap II
Suppose that you entered the following commands: counter = 1; for i = [1:10] for j = [1:10] M(i,j) = counter; counter = counter+1; end end for j = [1:100] C(j,1) = j/100; C(j,2) = 0; C(j,3) = 0; end imagesc(M) colormap(C) Which is the correct image?


2.10. Colormap III
Suppose that you entered the following commands: counter = 1; for i = [1:10] for j = [1:10] M(i,j) = counter; counter = counter+1; end end for j = [1:100] C(j,1) = 0; C(j,2) = j/100; C(j,3) = 0; end imagesc(M) colormap(C) Which is the correct image?


2.11. Colormap IV
Which of the following matrices was used as an input to imagesc
in order to create the plot shown below?
Answer 

B. The highest number is in the lowerright corner of the image and they decrease from 100 to 90 as you move to the left. The lowest number is in the upperleft corner. 
A. M = 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 
B. M = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 
C. M = 10 1 2 3 4 5 6 7 8 9 20 11 12 13 14 15 16 17 18 19 30 21 22 23 24 25 26 27 28 29 40 31 32 33 34 35 36 37 38 39 50 41 42 43 44 45 46 47 48 49 60 51 52 53 54 55 56 57 58 59 70 61 62 63 64 65 66 67 68 69 80 71 72 73 74 75 76 77 78 79 90 81 82 83 84 85 86 87 88 89 100 91 92 93 94 95 96 97 98 99 
D. M = 10 1 2 3 4 5 6 7 8 9 70 61 62 63 64 65 66 67 68 69 20 11 12 13 14 15 16 17 18 19 80 71 72 73 74 75 76 77 78 79 90 81 82 83 84 85 86 87 88 89 100 91 92 93 94 95 96 97 98 99 30 21 22 23 24 25 26 27 28 29 40 31 32 33 34 35 36 37 38 39 50 41 42 43 44 45 46 47 48 49 60 51 52 53 54 55 56 57 58 59 
2.12. Colormap VI
C = [1 1 1;0 0 0]; for i = [1:10] for j = [1:10] M(i,i) = 1; % Option A. M(i,j) = 1; % Option B. M(i,1) = 1; % Option C. M(i,2) = 1; % Option C. M(1,j) = 1; % Option D. M(2,j) = 1; % Option D. end end imagesc(M) colormap(C) axis square; colorbar;

