【问题标题】:Matlab save individual subplot as jpgMatlab将单个子图保存为jpg
【发布时间】:2013-10-05 19:28:40
【问题描述】:

for 循环中,我创建了可变数量的子图,这些子图显示在一个图形上。我还可以将每个子图另存为单独的全尺寸图和图像文件(最好是 JPG)吗?

【问题讨论】:

    标签: matlab plot subplot handles save-as


    【解决方案1】:

    假设你有subfigure的轴的句柄ha,你可以使用getframeframe2im如下,

    F = getframe(ha);
    [im,map] = frame2im(F);
    if isempty(map)
        imwrite(im,'subframe.jpg');
    else
        imwrite(im,map,'subframe.jpg');
    end
    

    请注意,这将准确地保存轴在屏幕上的显示方式,因此请在保存之前根据自己的喜好调整图形大小。要尽可能多地使用图形空间,请在 MATLAB Central 上试用 subfigure_tight 函数。

    【讨论】:

    • 谢谢 chappjc。它确实像宣传的那样,不幸的是我的框架返回空的颜色图。我会继续调查的。我给了你一个有用的点击。
    • 你是说颜色
    【解决方案2】:

    使用 copyobj 到一个新图形,然后使用带有新图形句柄的 saveas:

    您应该提供的示例代码(请参阅SSCCE):

    figure
    nLines = 2;
    nColumns = 3;
    handles = zeros(nLines,nColumns)
    for line = 1:nLines
      for column = 1:nColumns
        handles(line,column)=subplot(nLines,nColumns,column+(line-1)*nColumns);
        plot([line column]);
        title(sprintf('Cool title (%d,%d)',line,column))
        ylabel(sprintf('Ylabel yeah (%d,%d)',line,column))
        xlabel(sprintf('Xlabel nah (%d,%d)',line,column))
      end
    end
    

    这里我保存了子图句柄,但假设您没有保存它们:

    axesH = findobj(gcf,'Type','axes','-not','Tag','legend'); % This will change the order from which the axes will be saved. If you need to save in the correct order, you will need access to the subplot handles
    nAxes = numel(axesH)
    newFig = figure;
    for k=1:nAxes
       newAxes=copyobj(axesH(k),newFig);
       % Expand subplot to occupy the hole figure:
       set(newAxes,'OuterPosition',[0 0 1 1]);
       tightInset=get(newAxes,'TightInset');
       set(newAxes,'Position',[tightInset(1:2) [1 1]-(tightInset(3:4)+tightInset(1:2))])
       saveas(newFig,sprintf('axes_%d.jpg',k),'jpg');
       delete(newAxes);
    end
    delete(newFig);
    

    保存一个轴的示例:

    为了删除死区,我使用了可用的信息on this topic

    【讨论】:

    • 感谢华纳。我最终得到了一个单独的子图和颜色图文件。我给了你绿色的勾号,向我展示了如何枚举图形上的对象。我会继续调查的。
    • @user1502755 我不知道子图和颜色图有什么问题。如果您从正在使用的子图中提供 SSCCE 以及如何重现您遇到的问题,我可能会为您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2011-03-07
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多