这里给出了一个子图的例子:
http://www.mathworks.com/support/solutions/en/data/1-16BSF/?product=SL&solution=1-16BSF
figure(1)
surf(peaks(10))
colorbar
figure(2)
mesh(peaks(10))
colorbar
figure(3)
contour(peaks(10))
colorbar
figure(4)
pcolor(peaks(10))
colorbar
% Now create destination graph
figure(5)
ax = zeros(4,1);
for i = 1:4
ax(i)=subplot(4,1,i);
end
% Now copy contents of each figure over to destination figure
% Modify position of each axes as it is transferred
for i = 1:4
figure(i)
h = get(gcf,'Children');
newh = copyobj(h,5)
for j = 1:length(newh)
posnewh = get(newh(j),'Position');
possub = get(ax(i),'Position');
set(newh(j),'Position',...
[posnewh(1) possub(2) posnewh(3) possub(4)])
end
delete(ax(i));
end
figure(5)
如何在此示例中为子图添加标签?只需添加“图1”,“图2”等就具有指导意义。
答案 0 :(得分:4)
我想很多人会遇到这个条目,以便找到一种方法来简单地将标题添加到子图中而不进行任何复制(就像我一样)。对于这种情况,可以很容易地完成,如Sanjay Manohar所述:
figure(1)
subplot(4,1,1)
surf(peaks(10))
title('Figure 1') % must come AFTER the plot command
colorbar
subplot(4,1,2)
mesh(peaks(10))
title('Figure 2') % must come AFTER the plot command
colorbar
subplot(4,1,3)
contour(peaks(10))
title('Figure 3') % must come AFTER the plot command
colorbar
subplot(4,1,4)
pcolor(peaks(10))
title('Figure 4') % must come AFTER the plot command
colorbar
这里的重要部分是(我认为这是大多数错误的来源)title
- 命令必须在实际的绘图命令之后。如果是在绘制图形之前写的,标题就不会出现!
答案 1 :(得分:1)
你能否使用
figure(5)
subplot(4,1,1)
title('first figure')
subplot(4,1,2)
...
脚本末尾的?或者我错过了什么?
或者在原始数字中使用title
,例如
figure(1)
surf(peaks(10))
title('first figure')
答案 2 :(得分:1)
在脚本末尾添加两行,如下所示:
string = {'Figure 1','Figure 2','Figure 3','Figure 4'}; %%% or any titles you want
for i = 1:4
figure(i)
title(string{i}) %%% add this line
h = get(gcf,'Children');
newh = copyobj(h,5)
for j = 1:length(newh)
posnewh = get(newh(j),'Position');
possub = get(ax(i),'Position');
set(newh(j),'Position',...
[posnewh(1) possub(2) posnewh(3) possub(4)])
end
delete(ax(i));
end
figure(5)