[英]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.