如何在matlab中为图下方的每个图像添加字幕

时间:2015-11-21 11:10:38

标签: matlab matlab-figure

我正在使用子图在图中显示多个图像。我想在每张图像下面添加副标题(a),(b),...(c),如下图所示:

enter image description here

你能帮忙在MATLAB中写吗?这是上图的当前代码。如果可能的话,让我知道如何控制图像和子标题之间的边距。感谢

%# read images in a cell array
imgs = cell(6,1);
for i=1:6
    imgs{i} = imread( sprintf('AT3_1m4_%02d.tif',i) );
end

%# show them in subplots
figure(1)
for i=1:6
    subplot(2,3,i);
    h = imshow(imgs{i}, 'InitialMag',100, 'Border','tight');
    title(num2str(i))
    set(h, 'ButtonDownFcn',{@callback,i})
end

更新:我也尝试过使用subaxis lib。但是,结果不显示标题

%% read images in a cell array
imgs = cell(6,1);
for i=1:6
    imgs{i} = imread( sprintf('AT3_1m4_%02d.tif',i) );
end

figure(1)
spaceH=0.01;spaceV=0.1;marTop=0.3;marBot=0.0; 
 padding=0.0;margin=0.0;marginL=0.0;
for i=1:6
    subaxis(2,3,i,'SpacingHoriz', spaceH, ...
            'SpacingVert',spaceV, 'PL',padding,'PR',padding,'mt',... 
            marTop,'mb',marBot,'ML',marginL,'MR',margin); title('Original'); 
    imagesc(uint8(imgs{i}),[0 255]),colormap(gray),axis off;axis equal,
    xlabel(['(' char(96+1) ')']);
end

3 个答案:

答案 0 :(得分:2)

试试这个。要指定保证金,只需将“保证金”变量设置为您需要的任何值。

% show images with subtitles
figure(1);
margin = 0; % margin between title and image (in ex)
title_names = {'(a)','(b)','(c)','(d)','(e)','(f)'};
for i=1:6
    subplot(2,3,i);
    handle_image = imshow(imgs{i}, 'InitialMag',100, 'Border','tight');
    handle_title = title(title_names{i});
    set(handle_image, 'ButtonDownFcn',{@callback,i})

    % adjust title position -- in 2 steps

    % first set the title position to the bottom of the image
    image_height = get(handle_image,'YData');
    image_height(1) = [];
    position = get(handle_title,'Position');
    position(2) = image_height;
    set(handle_title,'Position',position);

    % then move it out of the image and apply some margin
    set(handle_title,'Units','characters');
    position = get(handle_title,'Position');
    position(2) = position(2) - margin - 1;
    set(handle_title,'Position',position);

    % EDIT: this line will keep titles on their relative position
    set(handle_title,'Units','normalized');
end

请注意,您必须在同一命名空间中定义@callback,否则每当您点击图片时都会抛出错误。

这是使用'trees.tiff'和0.5ex边距的示例结果。 example image, showing 6 subplots and their according subtitles

编辑:如果你想动态改变数字大小,标题不会在x方向上调整。为了得到类似的行为,经典的'标题'添加

set(handle_title,'Units','normalized');

低于所有其他线路。这将使标题保持在图中相同的相对坐标,而不是相同的固定坐标。

答案 1 :(得分:1)

首先,为了控制子图边距和其他属性,我强烈推荐可以在Mathworks文件交换中找到的tight_subplot函数。

关于添加标签的具体问题,您可以使用xlabel。例如:

figure(1)
for i=1:6
    subplot(2,3,i);
    h = plot(1:20, randn(20,1));
    xlb{i} = xlabel(['(' char(96+i) ')']);
end

您可以通过调整标签的Position属性来控制标签的位置,这些属性实际上是text个对象。例如:

% Move all xlabels down by yoffset
yoffset = 0.3;
for ii = 1:6
    xp = get(xlb{ii}, 'position');
    xp(2) = xp(2) - yoffset; % update y-position
    set(xlb{ii}, 'position', xp); % assign new position
end

答案 2 :(得分:0)

阅读mikkola解决方案后。我要感谢你的帮助。我想发布可能对其他人有帮助的解决方案 这是我的解决方案

%% read images in a cell array
imgs = cell(6,1);
for i=1:6
    imgs{i} = imread( sprintf('AT3_1m4_%02d.tif',i) );
end

figure(1)
spaceH=0.01;spaceV=0.06;marTop=0.3;marBot=0.08; 
 padding=0.0;margin=0.0;marginL=0.0;
 yoffset = 12;
for i=1:6
    subaxis(2,3,i,'SpacingHoriz', spaceH, ...
            'SpacingVert',spaceV, 'PL',padding,'PR',padding,'mt',... 
            marTop,'mb',marBot,'ML',marginL,'MR',margin); title('Original'); 
%     imagesc(uint8(imgs{i}),[0 255]),colormap(gray);axis equal,
   imshow(imgs{i}, 'InitialMag',90, 'Border','tight');
   xlb{i} = xlabel(['(' char(96+i) ')'],'FontSize', 16);
   xp = get(xlb{i}, 'position');
   xp(2) = xp(2) - yoffset; % update y-position
   set(xlb{i}, 'position', xp); % assign new position
end