matlab - 如何在 matlab 中为下图的每张图片添加字幕

标签 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 库。但是,结果不显示标题

%% 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 边距的示例结果。 example image, showing 6 subplots and their according subtitles

编辑:如果您想动态更改图形大小,则标题不会在 x 方向上调整。要获得类似的行为,请添加经典的“标题”

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

在所有其他行之下。这将使标题在图中保持相同的相对坐标,而不是相同的固定坐标。

关于matlab - 如何在 matlab 中为下图的每张图片添加字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33842616/

相关文章:

Matlab将图形保存为特定大小?

matlab - 使用 MATLAB 的 GPU 功能计算 sum(a.*exp(b.*c),1) 的有效方法

Matlab - ode45 - 如果不将步长减小到时间 t 允许的最小值 (1.136868e-13) 以下,则无法满足积分容差。

MATLAB 遇到内部错误,需要关闭

MATLAB 发现最大值。结构的

matlab - 如何使用 MATLAB 创建包含文本和图形的报告

matlab - MATLAB 中虚部为空时的复数可视化

MATLAB:将水平缩放设为默认值

matlab - 在不保存或删除图形的情况下保存 Matlab 工作区

matlab - 以不同颜色绘制 x 轴上方的 y 值