poster
2019-12-10, 20:48
我想使用MATLAB从双精度矩阵创建这样的图像。
样本图片: https://i.stack.imgur.com/vOUcK.jpg
http://twitpic.com/2xs943
回答:
您可以使用内置函数imagesc (https://www.mathworks.com/help/matlab/ref/imagesc.html)和text (https://www.mathworks.com/help/matlab/ref/text.html)并为图形对象调整许多参数,从而轻松地自己创建此类绘图。这是一个例子:
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]); 这是生成的图:
https://i.stack.imgur.com/lwjWc.png
如果您在使用x轴刻度标签时遇到麻烦,则选择太宽且彼此重叠,请按以下方法处理:
较新版本的MATLAB:不确定要添加哪个版本,但是在较新版本中,轴对象现在具有属性 (https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#property_d119e52325) '{X|Y|Z}TickLabelRotation' ,该属性 (https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#property_d119e52325)使您可以旋转标签并使它们更好地适合它们。
较旧的MATLAB版本:对于较旧的版本,您可以在MathWorks File Exchange (https://www.mathworks.com/matlabcentral/fileexchange/)上找到一些可以旋转刻度线标签文本的提交,例如Brian Brian的 (https://www.mathworks.com/matlabcentral/profile/authors/16217-brian-katz) XTICKLABEL_ROTATE (https://www.mathworks.com/matlabcentral/fileexchange/3486-xticklabel-rotate) 。
更多&回答... (https://stackoverflow.com/questions/3942892)
样本图片: https://i.stack.imgur.com/vOUcK.jpg
http://twitpic.com/2xs943
回答:
您可以使用内置函数imagesc (https://www.mathworks.com/help/matlab/ref/imagesc.html)和text (https://www.mathworks.com/help/matlab/ref/text.html)并为图形对象调整许多参数,从而轻松地自己创建此类绘图。这是一个例子:
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]); 这是生成的图:
https://i.stack.imgur.com/lwjWc.png
如果您在使用x轴刻度标签时遇到麻烦,则选择太宽且彼此重叠,请按以下方法处理:
较新版本的MATLAB:不确定要添加哪个版本,但是在较新版本中,轴对象现在具有属性 (https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#property_d119e52325) '{X|Y|Z}TickLabelRotation' ,该属性 (https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#property_d119e52325)使您可以旋转标签并使它们更好地适合它们。
较旧的MATLAB版本:对于较旧的版本,您可以在MathWorks File Exchange (https://www.mathworks.com/matlabcentral/fileexchange/)上找到一些可以旋转刻度线标签文本的提交,例如Brian Brian的 (https://www.mathworks.com/matlabcentral/profile/authors/16217-brian-katz) XTICKLABEL_ROTATE (https://www.mathworks.com/matlabcentral/fileexchange/3486-xticklabel-rotate) 。
更多&回答... (https://stackoverflow.com/questions/3942892)