【问题标题】:Producing subplots and then combine them into a figure later in MATLAB生成子图,然后稍后在 MATLAB 中将它们组合成一个图形
【发布时间】:2010-10-24 15:29:52
【问题描述】:

我的程序在命令循环期间产生小数字。有没有办法只保存这些数字,然后将它们组合成一个数字?

【问题讨论】:

    标签: matlab plot


    【解决方案1】:

    考虑代码:

    hFig = figure;
    
    %# create temporary subplots as template
    for i=1:2, h(i) = subplot(2,1,i); end       %# create subplots
    pos = get(h, 'Position');                   %# record their positions
    delete(h)                                   %# delete them
    
    %# load the .fig files inside the new figure
    fileNames = {'a.fig' 'b.fig'};              %# saved *.fig file names
    for i=1:2
        %# load fig
        hFigFile = hgload( fileNames{i} );
    
        %# move/copy axis from old fig to new fig
        hAx = get(hFigFile, 'Child');           %# hAx = gca;
        set(hAx, 'Parent',hFig)
        %#hAx = copyobj(hAx,hFig);
    
        %# resize it to match subplot position
        set(hAx, 'Position',pos{i});
    
        %# delete old fig
        delete(hFigFile)
    end
    

    本文改编自newsgroup discussion

    【讨论】:

    • 太棒了,真的很好。非常感谢,这样可以减轻很多痛苦。
    • 和新闻组讨论的链接很有用
    【解决方案2】:

    使用saveas。将您的子图保存为 FIG 文件,以便您以后可以完全控制它(而不是 JPG)。

    选择一种平铺图案,然后使用subplot 将多个图形合二为一。

    【讨论】:

    • 如何将它们加载到单个图形中?
    【解决方案3】:

    我这里有个答案作为例子:

    h1 = figure(1)
    plot(1:10,'o-r');
    title('title');
    xlabel('xlabel');
    ylabel('ylabel');
    
    % Copy contents
    ch(1) = copyobj(gca,gcf);
    
    % Figure 2
    h2 = figure(2)
    plot(1:30,'o-r');
    title('title fig2');
    xlabel('xlabel');
    ylabel('ylabel');
    % copy contents
    ch(2) = copyobj(gca,gcf);
    
    figure(3)
    sh = subplot(1,2,1);
    clear axes
    p = get(sh,'position');
    ah = copyobj(ch(1),gcf);
    set(ah,'position',p);
    
    % Create axis template
    sh = subplot(1,2,2);
    clear axes
    p = get(sh,'position');
    ah = copyobj(ch(2),gcf);
    set(ah,'position',p);
    
    % Delete template
    % delete(sh);
    

    【讨论】:

      【解决方案4】:

      Amro's solution 效果很好,但是对于箱线图,您必须重置 Xtick 和 Xtick 标签,否则,由于某种原因,它们不会根据子图调整大小。创建箱线图或打开图后添加:

      set(gca,'XTick',<1d vector>,'XTickLabel',<1d cell vector>)
      

      或放置自动刻度和标签

      set(gca,'XTickMode','auto','XTickLabelMode','auto')
      

      【讨论】:

      • 有意思,谢谢分享。您是否尝试复制而不是移动轴(我的代码中的注释行)?老实说,我的解决方案并非没有缺陷。例如,如果数字有不同的颜色图,它将失败(除非您使用 extra effort 来解决这个问题)
      猜你喜欢
      • 2019-04-09
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      相关资源
      最近更新 更多