Plot a matrix, values as colors

前端 未结 2 827
长情又很酷
长情又很酷 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:23

    I just padded the matrix prior to pcolor and I think it's the effect you wanted. The reason it works comes from the help doc for pcolor, which states that

    In the default shading mode, 'faceted', each cell has a constant color and the last row and column of C are not used.

    m = 12;
    n = 8;
    A = randi(5,[m n]);
    Arot = flipud(A);
    Arot = [ Arot; Arot(end,:) ];
    Arot = [ Arot, Arot(:,end) ];
    pcolor(Arot);figure(gcf);
    for i = 1 : n
      for j = 1 : m
        text(i + .5 , j + .5 ,num2str(Arot(j,i)),'FontSize',18);
      end
    end
    

提交回复
热议问题