[英]How can I add subtitle for each image below figure in matlab
我正在使用子图来显示图中的多个图像。 我想在每个图像下面添加副标题(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 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
试试这个。 要指定保证金,只需将“保证金”变量设置为您需要的任何值。
% 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');
低于所有其他线路。 这将使标题保持在图中相同的相对坐标处,而不是相同的固定坐标。
首先,为了控制子图边距和其他属性,我强烈推荐可以在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
属性来控制标签的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
读完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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.