如何解决如何在图像matlab之外查看文本
我想将矩阵的值表示为图像,在其边距中添加一些文本。为此,我按以下步骤操作:
matrix = rand(10); %Suppose this matrix
figure('color','w')
imshow(matrix,[min(matrix(:)),max(matrix(:))])
colormap(gca,jet(256))
colorbar
truesize([400,400])
for i=1:size(matrix,1)
text(-2,i,['long text ' num2str(i)],'Interpreter','none')
h = text(i,['column ' num2str(i)],'none');
set(h,'Rotation',90);
end
原图为:
我可以看到左边的文字增加了图形宽度(例如使用鼠标):
但是,如果我同时增加图形高度,图像也会增加,并且我无法同时看到左侧和顶部的全文。
我有两个问题:
- 有没有一种更简洁的方法来表示一个矩阵,包括像这个例子中那样的外部信息?
- 如何才能正确查看全文?
解决方法
对于您问题的第一部分,我会做这样的事情,这样您就不必跟踪标签的位置:
matrix = rand(10);
figure('color','w')
imagesc(matrix) % plot your matrix
axis equal % correct aspect-ratio
xlim([0.5,10.5])
colormap(gca,jet(256))
colorbar
% Create your labels
x = cell(1,size(matrix,1));
y = x;
for i = 1:size(matrix,1)
x{i} = sprintf('Column %u',i);
y{i} = sprintf('Long text %u',i);
end
% Edit your x axis
set(gca,...
'XTick',1:size(matrix,1),... % select the ticks to be displayed
'XTickLabel',x,... % select their labels
'XTickLabelRotation',90,... % rotate the labels vertically
'XAxisLocation','top'); % put the x axis on top
% Similar for the y axis
set(gca,...
'YTick',... % select the ticks to be displayed
'YTickLabel',y); % select their labels
对于您的第二个问题,可能有一种方法可以自动检测轴的最佳大小,但您可以简单地通过反复试验来完成:
set(gca,'OuterPosition',[0,0.05,1,0.9]) % for example?
特别是,您可能希望更改矢量的第一个和第三个元素以修改水平位置和宽度(以图形大小的分数表示),以及第二个和第四个元素以修改垂直位置和高度。>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。