How to plot with specific colors the values of a matrix in matlab

天大地大妈咪最大 提交于 2019-12-04 05:35:50

问题


I am working in matlab and I have a matrix which I would like to visualize it by giving a green-ish color to the lowest values per column/row and red-ish to the rest or different levels of red-ish depending on how close or far this values is from the lowest one and lastly print the values within. For example lets say that I have the following matrix:

0.0085 0.0244 0.0335 0.0312 0.0392 0.0392
0.0246 0.0078 0.0234 0.0281 0.0395 0.0395
0.0299 0.0295 0.0108 0.0224 0.0598 0.0598
0.0253 0.0317 0.0236 0.0123 0.0583 0.0583
0.0363 0.0337 0.0500 0.0497 0.0038 0.0583

what I want to achieve is something like this:

is there anyhow a way to achieve the above result?

I was checking on pcolor or imagesc functions but I couldn't get along with something. I found these links here and here that they kind of try to do something similar but I couldn't get it to change it to my needs. Therefore, does anybody know if it is possible something like that to be done and how?

Thanks.


Update:

In case that I want to add also some titlebars on the top and on the side, look below:


回答1:


Here is a quick option:

A = [0.0085 0.0244 0.0335 0.0312 0.0392 0.0392
    0.0246 0.0078 0.0234 0.0281 0.0395 0.0395
    0.0299 0.0295 0.0108 0.0224 0.0598 0.0598
    0.0253 0.0317 0.0236 0.0123 0.0583 0.0583
    0.0363 0.0337 0.0500 0.0497 0.0038 0.0583];
back = [1 0 0];
headers = [0.5 0.5 0.5];
minima = [0 1 0];
map = [back ; headers; minima];
colormap(map)
[~,ind] = min(A);
B = zeros(size(A));
for k = 1:size(A,2)
    B(ind(k),k) = 1;
end
B = [ones(1,size(B,2))*0.5;B];
B = [ones(size(B,1),1)*0.5 B];
imagesc(B)
axis off
[y,x]=ndgrid((1:size(A,1)),(1:size(A,2)));
row_titles = num2str((1:size(A,1)).'); % could be any vector...
text(ones(size(A,1),1),2:size(A,1)+1,row_titles,'FontSize',16,'HorizontalAlignment','center',...
    'VerticalAlignment','middle','Color','w')
coloumn_titles = num2str((1:size(A,2)).'); % could be any vector...
text(2:size(A,2)+1,ones(size(A,2),1),coloumn_titles,'FontSize',16,'HorizontalAlignment','center',...
    'VerticalAlignment','middle','Color','w')
text(x(:)+1,y(:)+1,num2str(A(:)),'FontSize',16,'HorizontalAlignment','center',...
    'VerticalAlignment','middle')

which gives:

and you can change the headers, back and minima colors to fit your prefered style.




回答2:


You can find the minimum on each column, and create a matrix that associates a colour with the corresponding index of the minimum.

This example creates a color for the minima of each column.

[~, idx] = min(A);
M = zeros(size(A));
for iCol = 1:size(A,2)
    M(idx(iCol), iCol) = 1;
end
imagesc(M);

Similarly, you can create a function to assign the desired colour to each value. If you each to print the string of the values on, you can use the text function.




回答3:


You can create it using insertText.

Use something like the following sample:

A = [0.0085 0.0244 0.0335 0.0312 0.0392 0.0392
     0.0246 0.0078 0.0234 0.0281 0.0395 0.0395
     0.0299 0.0295 0.0108 0.0224 0.0598 0.0598
     0.0253 0.0317 0.0236 0.0123 0.0583 0.0583
     0.0363 0.0337 0.0500 0.0497 0.0038 0.0583];

I = zeros(92, 348, 3, 'uint8');
I(:,:,1) = 255;I(:,:,2) = 199;I(:,:,3) = 206; %Background color

text_str = cell(length(A(:)), 1);
box_color = zeros(length(A(:)), 3);
text_color = zeros(length(A(:)), 3);
position = zeros(length(A(:)), 2);
for y = 0:size(A, 1)-1
    for x = 0:size(A,2)-1
        index = y*size(A,2) + x + 1;
        val = A(y+1, x+1);
        text_str{index} = [' ', num2str(val, '%0.4f'), ' '];
        position(index, 1) = x*58 + 1;
        position(index, 2) = y*18 + 1;
        if (val == min(A(:, x+1)))
            %Green
            box_color(index, :) = uint8([198, 239, 206]);
            text_color(index, :) = uint8([0, 100, 0]);
        else
            %Red
            box_color(index, :) = uint8([255, 199, 206]);
            text_color(index, :) = uint8([100, 0, 0]);
        end
    end
end

I = insertText(I, position, text_str, 'FontSize', 12, 'BoxColor', box_color, 'TextColor', text_color, 'BoxOpacity', 1);

figure;imshow(I);

Note: In newer version of Matlab (newer than I used), you can also select the font.



来源:https://stackoverflow.com/questions/39516274/how-to-plot-with-specific-colors-the-values-of-a-matrix-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!