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

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

问题描述

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

示例图像:

'{X|Y|Z}TickLabelRotation',这允许您旋转标签并更好地适应它们.

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

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

    Sample image:

    http://twitpic.com/2xs943

    解决方案

    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:

    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.

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

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