如何在 matlab 中为下图的每张图片添加字幕
How can I add subtitle for each image below figure in matlab
我正在使用子图在图中显示多个图像。我想在每张图片下面加上副标题(a),(b),...(c) 如下图:
能帮忙用MATLAB写吗?这是上图的当前代码。如果可能的话,让我知道如何控制图像和 sub-title 之间的边距。谢谢
%# 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
试试这个。要指定边距,只需将 'margin' 变量设置为您需要的值即可。
% 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 方向上调整。要获得类似的行为,请使用经典 'titles' 添加
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
属性来控制标签的位置,实际上是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 的解决方案后。我想感谢你的帮助。我想 post 我的解决方案可能对其他人有帮助
这是我的解决方案
%% 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
我正在使用子图在图中显示多个图像。我想在每张图片下面加上副标题(a),(b),...(c) 如下图:
能帮忙用MATLAB写吗?这是上图的当前代码。如果可能的话,让我知道如何控制图像和 sub-title 之间的边距。谢谢
%# 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
试试这个。要指定边距,只需将 'margin' 变量设置为您需要的值即可。
% 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 方向上调整。要获得类似的行为,请使用经典 'titles' 添加
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
属性来控制标签的位置,实际上是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 的解决方案后。我想感谢你的帮助。我想 post 我的解决方案可能对其他人有帮助 这是我的解决方案
%% 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