我正在使用子图在图中显示多个图像。我想在每个图像下面添加副标题 (a),(b),...(c) 如下图:
你能帮忙用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 库。但是,结果不显示标题
%% 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
最佳答案
试试这个。要指定边距,只需将“边距”变量设置为您需要的任何值即可。
% 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 边距的示例结果。
编辑:如果您想动态更改图形大小,则标题不会在 x 方向上调整。要获得类似的行为,请添加经典的“标题”
set(handle_title,'Units','normalized');
在所有其他行之下。这将使标题在图中保持相同的相对坐标,而不是相同的固定坐标。
关于matlab - 如何在 matlab 中为下图的每张图片添加字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33842616/