【问题标题】:Labeling Subplots in Matlab在 Matlab 中标记子图
【发布时间】:2013-07-29 15:39:48
【问题描述】:

这里给出了一个子图的例子:

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”等即可。

【问题讨论】:

  • 您要添加什么样的标签?
  • 在我的应用程序中,我正在创建一组文件内容的直方图,并希望用文件名标记每个直方图,即'CFZ12'

标签: matlab subplot


【解决方案1】:

我想很多人会遇到这个条目,以便找到一种方法来简单地将标题添加到子情节而无需任何复制(就像我一样)。对于这种情况,正如 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-command 必须出现在实际的 plot 命令之后。如果是在绘制图形之前写的,标题就不会出现!

【讨论】:

    【解决方案2】:

    在脚本末尾添加两行,如下所示:

    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)
    

    【讨论】:

      【解决方案3】:

      你不能用吗

      figure(5)
      subplot(4,1,1)
      title('first figure')
      subplot(4,1,2)
      ...
      

      在脚本的末尾?还是我错过了什么?

      或者在原始图中使用title,例如

       figure(1)
       surf(peaks(10))
       title('first figure')
      

      【讨论】:

      • 您的第一个建议清除了数字(至少在某些版本的 Matlab 中)。第二个不起作用,因为没有复制标题
      猜你喜欢
      • 2014-01-16
      • 2017-08-28
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      相关资源
      最近更新 更多