【问题标题】:How to see text outside image matlab如何在图像matlab之外查看文本
【发布时间】:2021-07-19 10:59:58
【问题描述】:

我想将矩阵的值表示为图像,并在其边距中添加一些文本。为此,我进行如下操作:

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,0,['column ' num2str(i)], 'Interpreter', 'none');
    set(h,'Rotation',90);
end

原图为:

我可以看到左边的文字增加了图形的宽度(例如使用鼠标):

但是,如果我也增加图形高度,图像会增加,我无法同时看到左侧和顶部的全文。

我有两个问题:

  1. 有没有更简洁的方法来表示包含外部信息的矩阵,如本例所示?
  2. 如何才能正确查看全文?

【问题讨论】:

    标签: matlab matlab-figure figure


    【解决方案1】:

    对于您问题的第一部分,我会这样做,这样您就不必跟踪标签的位置:

    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', 1:size(matrix, 1), ... % select the ticks to be displayed
        'YTickLabel', y);               % select their labels
    

    对于您的第二个问题,可能有一种方法可以自动检测轴的最佳尺寸,但您可以通过反复试验简单地做到这一点:

    set(gca, 'OuterPosition', [0, 0.05, 1, 0.9]) % for example?
    

    特别是,您可能希望更改向量的第一个和第三个元素以修改水平位置和宽度(以图形大小的分数),并更改第二个和第四个元素以修改垂直位置和高度。

    【讨论】:

      猜你喜欢
      • 2013-07-19
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 2018-12-11
      • 1970-01-01
      相关资源
      最近更新 更多