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

时间:2010-10-15 13:57:31

标签: matlab matrix data-visualization

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

示例图片: alt text

http://twitpic.com/2xs943

4 个答案:

答案 0 :(得分:89)

您可以使用内置函数imagesctext轻松自己创建此类图,并调整图形对象的多个参数。这是一个例子:

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]);

这是生成的数字:

alt text

如果您在使用x轴刻度标签遇到问题时选择过宽且彼此重叠,请按以下步骤操作:

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

  • 旧版本的MATLAB:对于旧版本,您可以在MathWorks File Exchange上找到一些可以旋转刻度标签文字的提交内容,例如XTICKLABEL_ROTATE来自Brian Katz 3}}

答案 1 :(得分:18)

h = imagesc(magic(8))
impixelregion(h)

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

需要图像处理工具箱 alt text

答案 2 :(得分:1)

我希望你能说服Matlab画出来,如果你看一下File Exchange,你可能会发现有人已经编写了代码。但如果您没有代码,使用MS Excel会更容易。

编辑:所以我给了这个更多的想法,这就是我想出来的。我没有掌握向SO发布图形,所以相信我,这将引导您找到解决方案。但用Excel真的会更容易。

首先使用您的数据值定义矩阵;我在下面调用矩阵G。然后执行命令:

image(G); 
colormap(gray)

现在,我不得不做一些摆弄,重新调整数据,以获得一个好的图形,但这应该产生一个数字轴的灰度图。现在,转到图窗口并打开绘图工具。

选择X轴并点击Ticks按钮。您现在要做的就是将标签编辑为您想要的文本。对Y轴执行相同操作。在图上的方块中写下数字 - 使用Annotations菜单中的Text Box。

经过大量的摆弄,你会得到你想要的图形。此时,我建议你选择菜单命令File |生成M-File并做到这一点。如果你想在未来以编程方式创建这样的图形,只需将生成的M文件转换为适合你想要的函数。

但在Excel中它仍然容易得多。

答案 3 :(得分:1)

如果您只关心在矩阵中查看零/非零条目(例如,如果它是稀疏的),请使用spy

否则,请使用imagesc

PS:我无法访问您的图片