如何可视化具有显示的颜色和值的矩阵? [英] How do I visualize a matrix with colors and values displayed?

查看:79
本文介绍了如何可视化具有显示的颜色和值的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

样本图片:

http://twitpic.com/2xs943

推荐答案

您可以使用内置函数 text 并为图形对象调整许多参数.这是一个示例:

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

这是生成的数字:

如果您在选择x轴刻度标签时遇到麻烦,则选择它们太宽且彼此重叠,可以按照以下方法进行处理:

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:

  • MATLAB的较新版本:不确定添加了哪个版本,但是在较新版本中,轴对象现在具有

  • 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.

MATLAB的较旧版本:对于较旧的版本,您可以在 Brian Katz .

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.

这篇关于如何可视化具有显示的颜色和值的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆