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

标签 matlab matrix data-visualization

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

示例图片: alt text

http://twitpic.com/2xs943

最佳答案

您可以使用内置函数 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]);

这是生成的图形:

alt text

如果您遇到问题,因为您选择的 x 轴刻度标签太宽且彼此重叠,您可以按照以下方法处理:

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

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

关于matlab - 如何可视化显示颜色和值的矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3942892/

相关文章:

matlab - 我可以在匿名函数中重用表达式的结果吗?

Matlab:max(A,[],dim) 中的第二个参数是什么意思?

r - 避免在 R 中使用多个 for 循环来计算矩阵

python - 在 matplotlib 中将 x 轴移动到绘图的顶部

python - 是否可以更改 Plotly 中图例框的大小?

matlab - 如何取消隐藏被覆盖的函数?

matlab - 在重复的x位置上用y点进行曲线拟合(Galaxy螺旋臂)

matlab - 更改 Matlab 中的整列和行

python - 使用 python 替换字典中矩阵键返回错误的选项

r - 在 R 中可视化来自事后 Tukey 的临界值/成对比较