49

I want to create images like this from a double precision matrix using MATLAB.

Sample image: alt text

http://twitpic.com/2xs943

EJG89
  • 1,189
  • 7
  • 17
Piji
  • 685
  • 1
  • 8
  • 13

4 Answers4

91

You can create this sort of plot yourself pretty easily using the built-in functions imagesc and text and adjusting a number of parameters for the graphics objects. Here's an example:

mat = rand(5);           % A 5-by-5 matrix of random values from 0 to 1
imagesc(mat);            % Create a colored plot of the matrix values
colormap(flipud(gray));  % Change the colormap to gray (so higher values are
                         %   black and lower values are white)

textStrings = num2str(mat(:), '%0.2f');       % Create strings from the matrix values
textStrings = strtrim(cellstr(textStrings));  % Remove any space padding
[x, y] = meshgrid(1:5);  % Create x and y coordinates for the strings
hStrings = text(x(:), y(:), textStrings(:), ...  % Plot the strings
                'HorizontalAlignment', 'center');
midValue = mean(get(gca, 'CLim'));  % Get the middle value of the color range
textColors = repmat(mat(:) > midValue, 1, 3);  % Choose white or black for the
                                               %   text color of the strings so
                                               %   they can be easily seen over
                                               %   the background color
set(hStrings, {'Color'}, num2cell(textColors, 2));  % Change the text colors

set(gca, 'XTick', 1:5, ...                             % Change the axes tick marks
         'XTickLabel', {'A', 'B', 'C', 'D', 'E'}, ...  %   and tick labels
         'YTick', 1:5, ...
         'YTickLabel', {'A', 'B', 'C', 'D', 'E'}, ...
         'TickLength', [0 0]);

And here's the figure this generates:

alt text

If you run into trouble with the x-axis tick labels you choose being too wide and overlapping one another, here's how you can handle it:

  • Newer versions of MATLAB: Not sure which version this was added, but in newer versions axes objects now have the properties '{X|Y|Z}TickLabelRotation', which allow you to rotate the labels and fit them better.

  • Older versions of MATLAB: For older versions you can find some submissions on the MathWorks File Exchange that can rotate the tick label text, like XTICKLABEL_ROTATE from Brian Katz.

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • 4
    for a more general solution, you should compare against `mean(get(gca,'CLim'))` instead of `0.5` to determine the color of the text on a grayscale colormap background. – Amro Oct 15 '10 at 17:50
  • 1
    another remark (the last one I swear!): if the values have long and short strings (say `mat=rand(5)*1000; mat(1)=1; mat(2)=1000;`) the text will not be centered because of the spaces padded. You might want to do: `textStrings = strtrim( cellstr(textStrings) );` – Amro Oct 15 '10 at 18:16
  • @Amro: That's a good suggestion as well. I overlooked it because the first solution I posted actually created the strings directly from a cell array using CELLFUN, so there was no padding to begin with. I changed the solution to make it a little easier to read. – gnovice Oct 15 '10 at 18:23
  • Thanks a lot. I will user it and find some method to rotate xtick – Piji Oct 15 '10 at 23:21
  • I wish I could do more than upvoting. – guneykayim Jul 25 '14 at 00:02
  • Already the *Get the middle value of the color range + Choose white or black for the text color of the strings so they can be easily seen over the background color* deserves the upvote – embert Feb 25 '15 at 14:36
  • This is great! One mod for Octave: I had to add `textColors = +textColors` to cast from logical to double before setting the `hStrings` colors. I guess Octave must be more picky about the data type for that setting. – Nick Benes Apr 30 '15 at 19:06
18
h = imagesc(magic(8))
impixelregion(h)

http://www.mathworks.com/help/toolbox/images/ref/impixelregion.html

Requires Image Processing Toolbox alt text

gnovice
  • 125,304
  • 15
  • 256
  • 359
MatlabDoug
  • 5,704
  • 1
  • 24
  • 36
1

I expect you could persuade Matlab to draw that, if you look at the File Exchange you may find someone has already written the code. But it would be a lot easier, if you don't have the code, to use MS Excel.

EDIT: So I gave this some more thought and here's what I came up with. I've not mastered posting graphics to SO, so trust me, this will lead you towards a solution. But it would honestly be easier with Excel.

First define a matrix with your data values; I call the matrix G in the following. Then execute the commands:

image(G); 
colormap(gray)

Now, I had to do some fiddling around, rescaling the data, to get a good graphic, but this should produce a gray-scale plot with numeric axes. Now, go to your figure window and open the plot tools.

Select the X axis and hit the Ticks button. All you have to do now is edit the labels to the texts that you want. Do the same for the Y axis. Write the numbers in the squares on the plot -- use the Text Box from the Annotations menu.

After a lot of fiddling about you'll have the graphic you want. At this point, I suggest that you choose the menu command File | Generate M-File and do just that. If you want to create such graphics programmatically in future just turn the generated M file into a proper function that does what you want.

But it's still a lot easier in Excel.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
1

If you only care about looking at zero/non-zero entries in your matrix (e.g. if it's sparse), use spy.

Else, use imagesc.

PS: I can't access your image

Jacob
  • 34,255
  • 14
  • 110
  • 165