如何使用显示的颜色和值来显示矩阵?

Pij*_*iji 48 matlab data-visualization matrix

我想用MATLAB从双精度矩阵中创建这样的图像.

示例图片: 替代文字

http://twitpic.com/2xs943

gno*_*ice 89

您可以使用内置函数轻松创建此类图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]);
Run Code Online (Sandbox Code Playgroud)

这是产生的数字:

替代文字

如果你遇到麻烦的x轴刻度标签,你选择太宽和相互重叠,这是你如何处理它:

  • 较新版本的MATLAB:不确定添加了哪个版本,但在较新版本中,轴对象现在具有属性 '{X|Y|Z}TickLabelRotation',允许您旋转标签并更好地适应它们.

  • 旧版本的MATLAB的:对于老版本,你可以找到一些材料MathWorks的文件交换,可旋转刻度标签文本,像XTICKLABEL_ROTATE布赖恩·卡茨.

  • 对于更通用的解决方案,您应该与`mean(get(gca,'CLim'))`而不是`0.5`进行比较,以确定灰度色彩图背景上文本的颜色. (4认同)

Mat*_*oug 18

h = imagesc(magic(8))
impixelregion(h)
Run Code Online (Sandbox Code Playgroud)

http://www.mathworks.com/help/toolbox/images/ref/impixelregion.html

需要图像处理工具箱 替代文字

  • +1:这是一种非常疯狂的方法.我希望OP尝试这个...... (2认同)