How to visualize a matrix with displayed colors and values?

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

Image example: alt text

http://twitpic.com/2xs943

+47
matrix matlab data-visualization
Oct. 15 '10 at 13:57
source share
4 answers

You can create this kind of plot quite easily using the built-in functions imagesc and text and setting a number of parameters for graphic objects. Here is 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 this figure generates:

alt text

If you encounter problems with the x-axis tick ticks, you select too wide and overlap each other, here's how you can handle this:

  • Newer versions of MATLAB: You do not know which version was added, but in newer versions, axes now have properties '{X|Y|Z}TickLabelRotation' , which allow you to rotate labels and fit better into them.

  • Old versions of MATLAB:. For older versions, you can find some materials in the MathWorks File Exchange , which can rotate label text labels, for example XTICKLABEL_ROTATE from Brian Katz .

+87
Oct. 15 2018-10-15
source share
 h = imagesc(magic(8)) impixelregion(h) 

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

Image processing panel required alt text

+17
Oct 15 2018-10-15
source share

I expect that you can convince Matlab to attract this, if you look at File Exchange, you may find that someone has already written the code. But it would be much easier if you didn’t have the code to use MS Excel.

EDIT: So, I gave you a few more thoughts and this is what I came up with. I have not mastered the placement of graphics in SO, so believe me, this will lead you to a solution. But with Excel it would be honestly easier.

First define a matrix with your data values; I call the matrix G as follows. Then run the commands:

 image(G); colormap(gray) 

Now I had to play a little, rescale the data to get a good graph, but this should lead to a gray graph with numerical axes. Now go to the shapes window and open the plot tools.

Select the X axis and click the Ticks button. All you have to do is change the labels to the texts you need. Do the same for the Y axis. Write the numbers in squares on the graph - use the text box in the Annotations menu.

After much debate that you will have the schedule you need. At this point, I suggest you select the menu command File | Create an M file and do just that. If you want to create such graphics programmatically in the future, simply turn the generated M file into the desired function, which will do what you want.

But Excel is still a lot easier.

+1
Oct. 15 2018-10-15
source share

If you are only interested in looking at null / non-null entries in your matrix (for example, if they are sparse), use spy .

Otherwise use imagesc .

PS: I can’t access your image

+1
Oct. 15 2018-10-15
source share



All Articles