MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何可视化显示颜色和值的矩阵? (https://www.labfans.com/bbs/showthread.php?t=24001)

poster 2019-12-10 20:48

如何可视化显示颜色和值的矩阵?
 
我想使用MATLAB从双精度矩阵创建这样的图像。

样本图片: [IMG]https://i.stack.imgur.com/vOUcK.jpg[/IMG]

[URL]http://twitpic.com/2xs943[/URL]



[B]回答:[/B]

您可以使用内置函数[URL="https://www.mathworks.com/help/matlab/ref/imagesc.html"]imagesc[/URL]和[URL="https://www.mathworks.com/help/matlab/ref/text.html"]text[/URL]并为图形对象调整许多参数,从而轻松地自己创建此类绘图。这是一个例子:

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]); 这是生成的图:

[IMG]https://i.stack.imgur.com/lwjWc.png[/IMG]

如果您在使用x轴刻度标签时遇到麻烦,则选择太宽且彼此重叠,请按以下方法处理:
[LIST][*] [B]较新版本的MATLAB:[/B]不确定要添加哪个版本,但是在较新版本中,轴对象现在具有[URL="https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#property_d119e52325"]属性[/URL] '{X|Y|Z}TickLabelRotation' ,该[URL="https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#property_d119e52325"]属性[/URL]使您可以旋转标签并使它们更好地适合它们。
[*] [B]较旧的MATLAB版本:[/B]对于较旧的版本,您可以在[URL="https://www.mathworks.com/matlabcentral/fileexchange/"]MathWorks File Exchange[/URL]上找到一些可以旋转刻度线标签文本的提交,例如[URL="https://www.mathworks.com/matlabcentral/profile/authors/16217-brian-katz"]Brian Brian的[/URL] [URL="https://www.mathworks.com/matlabcentral/fileexchange/3486-xticklabel-rotate"]XTICKLABEL_ROTATE[/URL] 。
[/LIST]
[url=https://stackoverflow.com/questions/3942892]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:31

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.