您可以使用内置函数
IMAGESC和
TEXT很容易地创建此类绘图,并调整图形对象的多个参数。这里有一个例子:
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