如何用颜色和值显示matrix?

我想使用MATLAB从双精度matrix创build像这样的图像。

示例图像: 替代文字

http://twitpic.com/2xs943

您可以使用内置函数imagesctext以及调整graphics对象的许多参数来轻松创build这种绘图。 这是一个例子:

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

这就是这个数字:

替代文字

如果您select的x轴刻度标签遇到麻烦,select的标签太宽而且彼此重叠,请按照以下步骤操作:

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

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

 h = imagesc(magic(8)) impixelregion(h) 

http://www.mathworks.com/help/toolboxhttp://img.dovov.comref/impixelregion.html

需要image processing工具箱 替代文字

如果你只关心在你的matrix中查看零/非零的条目(例如,如果稀疏),请使用spy

否则,使用imagesc

PS:我无法访问您的图片

我希望你可以说服Matlab来绘制,如果你看看文件交换,你可能会发现有人已经写了代码。 但是,如果你没有代码,使用MS Excel会更容易。

编辑:所以我给了这个更多的想法,这就是我想出了。 我还没有掌握发布graphics,所以相信我,这将导致你的解决scheme。 但是用Excel来说,这会更容易。

首先用你的数据值定义一个matrix; 我在下面称之为matrixG 然后执行命令:

 image(G); colormap(gray) 

现在,我不得不做一些摆弄,重新调整数据,以获得一个良好的graphics,但这应该产生一个数值轴的灰度图。 现在,进入你的数字窗口,打开绘图工具。

selectX轴并点击Ticksbutton。 你现在要做的就是把标签编辑到你想要的文本上。 对Y轴做同样的事情。 将数字写在图上的正方形中 – 使用“注解”菜单中的“文本框”。

经过很多的摆弄之后,你会得到你想要的graphics。 此时,我build议你select菜单命令File | 生成M文件,并做到这一点。 如果你想在未来编程创build这样的graphics,只需将生成的M文件转换为一个合适的function,即可实现所需function。

但是在Excel中它仍然更容易。