如何可视化显示颜色和值的矩阵?

发布于 2024-09-28 03:22:13 字数 200 浏览 4 评论 0原文

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

示例图片: alt text

http:// /twitpic.com/2xs943

I want to create images like this from a double precision matrix using MATLAB.

Sample image:
alt text

http://twitpic.com/2xs943

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫性小仙女 2024-10-05 03:22:13

您可以使用内置函数轻松创建此类绘图 imagesc文本 并调整图形对象的许多参数。这是一个示例:

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: 不确定添加了哪个版本,但在较新版本中,坐标区对象现在具有 属性 '{ X|Y|Z}TickLabelRotation',允许您旋转标签并使其更好地贴合。

  • 旧版 MATLAB: 对于旧版本,您可以在 MathWorks File Exchange 可以旋转刻度标签文本,例如 XTICKLABEL_ROTATE 来自 Brian Katz

You can create this sort of plot yourself pretty easily using the built-in functions imagesc and text and adjusting a number of parameters for the graphics objects. Here's an example:

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

And here's the figure this generates:

alt text

If you run into trouble with the x-axis tick labels you choose being too wide and overlapping one another, here's how you can handle it:

  • Newer versions of MATLAB: Not sure which version this was added, but in newer versions axes objects now have the properties '{X|Y|Z}TickLabelRotation', which allow you to rotate the labels and fit them better.

  • Older versions of MATLAB: For older versions you can find some submissions on the MathWorks File Exchange that can rotate the tick label text, like XTICKLABEL_ROTATE from Brian Katz.

想念有你 2024-10-05 03:22:13
h = imagesc(magic(8))
impixelregion(h)

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

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

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

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

Requires Image Processing Toolbox
alt text

眼中杀气 2024-10-05 03:22:13

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

否则,请使用 imagesc

PS:我无法访问你的图片

If you only care about looking at zero/non-zero entries in your matrix (e.g. if it's sparse), use spy.

Else, use imagesc.

PS: I can't access your image

初懵 2024-10-05 03:22:13

我希望你能说服Matlab画出这个,如果你看看文件交换你可能会发现有人已经写了代码。但如果您没有代码,使用 MS Excel 会容易得多。

编辑:所以我对此进行了更多思考,这就是我的想法。我还没有掌握向 SO 发布图形,所以相信我,这将引导您找到解决方案。但老实说,使用 Excel 会更容易。

首先用您的数据值定义一个矩阵;下面我将矩阵称为 G。然后执行命令:

image(G); 
colormap(gray)

现在,我必须做一些调整,重新缩放数据,才能获得良好的图形,但这应该会生成带有数字轴的灰度图。现在,转到图形窗口并打开绘图工具。

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

经过一番摆弄,你就会得到你想要的图形。此时,我建议您选择菜单命令“文件|”。生成 M 文件并执行此操作。如果您想将来以编程方式创建此类图形,只需将生成的 M 文件转换为执行您想要的操作的适当函数即可。

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

I expect you could persuade Matlab to draw that, if you look at the File Exchange you may find someone has already written the code. But it would be a lot easier, if you don't have the code, to use MS Excel.

EDIT: So I gave this some more thought and here's what I came up with. I've not mastered posting graphics to SO, so trust me, this will lead you towards a solution. But it would honestly be easier with Excel.

First define a matrix with your data values; I call the matrix G in the following. Then execute the commands:

image(G); 
colormap(gray)

Now, I had to do some fiddling around, rescaling the data, to get a good graphic, but this should produce a gray-scale plot with numeric axes. Now, go to your figure window and open the plot tools.

Select the X axis and hit the Ticks button. All you have to do now is edit the labels to the texts that you want. Do the same for the Y axis. Write the numbers in the squares on the plot -- use the Text Box from the Annotations menu.

After a lot of fiddling about you'll have the graphic you want. At this point, I suggest that you choose the menu command File | Generate M-File and do just that. If you want to create such graphics programmatically in future just turn the generated M file into a proper function that does what you want.

But it's still a lot easier in Excel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文