Plot a matrix, values as colors

前端 未结 2 854
长情又很酷
长情又很酷 2021-01-07 10:50

I have random matrix with arbitrary dimensions and I want to assign a color for each value (randomly or not) and plot the matrix with numbers like,

2条回答
  •  死守一世寂寞
    2021-01-07 11:20

    Using imagesc instead of pcolor solves the problem. It also brings some other benefits:

    • Avoids the need for flipud;
    • The coordinates of the text objects become integer values;
    • Axes are automatically set to "matrix" mode, with the origin in the upper right corner.

    Code:

    m = 8;
    n = 6;
    A = randi(5,[m n]);
    imagesc(A);
    for ii = 1:n
        for jj = 1:m
            text(ii, jj, num2str(A(jj,ii)), 'FontSize', 18);
        end
    end
    

    For

    A =
         4     5     4     2     4     4
         5     4     3     4     4     2
         5     4     1     1     1     3
         4     3     5     2     5     4
         1     2     2     2     5     3
         1     5     2     5     1     3
         4     3     1     3     3     1
         3     1     2     4     2     3
    

    this produces

    enter image description here

提交回复
热议问题