简体   繁体   中英

How can I add subtitle for each image below figure in matlab

I am using subplot to display multiple image in the a figure. I want to add the subtitle (a),(b),...(c) below each image as the below figure:

在此输入图像描述

Could you help to write it in MATLAB? This is current code for above image. if it is possible, let me know how to control the margin between image and the sub-title. Thanks

%# 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

Updated: I also tried with subaxis lib. However, the result do not show titles

%% 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

Try this. To specify a margin just set the 'margin' variable to whatever you need.

% 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

Note that you have to define @callback within the same namespace or it will throw an error, whenever you klick on an image.

This is an example result using 'trees.tiff' and a margin of 0.5ex. 示例图像,显示6个子图及其相应的字幕

EDIT: If you want to alter the figure size dynamicly the titles woun't adjust in x direction. To get a similar behavior then classic 'titles' add

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

below all other lines. This will keep the title at the same relative coordinates in the figure instead of the same fixed coordinates.

First of all, for controlling subplot margins and other properties, I highly recommend the tight_subplot function that may be found at the Mathworks File Exchange.

As for the specific question for adding labels, you can use xlabel . Example:

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

You can control the location of the label by tuning the Position property of the labels, which are actually text objects. Example:

% 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

After read solution of mikkola. I would like thank for your help. I would like to post my solution which may be helpful for other people This is my solution

%% 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM