最佳答案
您可以使用内置函数 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
% 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]);
这是生成的图形:
如果您遇到问题,因为您选择的 x 轴刻度标签太宽且彼此重叠,您可以按照以下方法处理:
较新版本的 MATLAB: 不确定这是哪个版本添加的,但在较新版本中,axes 对象现在具有 properties
'{X|Y|Z}TickLabelRotation'
,它允许您旋转标签并更好地适应它们。旧版本的 MATLAB:对于旧版本,您可以在 MathWorks File Exchange 上找到一些提交可以旋转刻度标签文本,如 XTICKLABEL_ROTATE来自 Brian Katz .
关于matlab - 如何可视化显示颜色和值的矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3942892/