matlab - 在 Matlab 中保存裁剪图

标签 matlab crop figure

我用 openfig 从 matlab 文件中打开了一个高度复杂的 .fig 图。

在此之后我用

裁剪了这个数字

轴([X1, X2, Y1, Y2])

我现在想保存裁剪后的图形,以便在保存时缩小尺寸。问题是 savefig 将保存缩放到指定轴的完整图像。如何保存图形,永久裁剪到我指定的轴?例如打开新图像时应该无法缩小。

最佳答案

我面前没有 MATLAB,我也没有你的数据来测试这个,所以这是一个大概的答案,但正如@natan 在上面的评论中提到的,你可以从存储在“XData”和“YData”中的图形。然后您可以将数据裁剪成您想要的部分,然后用裁剪后的数据替换现有数据。它最终看起来像这样:

kids = findobj(gca,'Type','line');
K = length(kids);

Xrange = get(gca,'XLim');
Yrange = get(gca,'YLim');

for k = 1:K
    X = get(kids(k),'XData');
    Y = get(kids(k),'YData');

    idx = X >= min(Xrange) & X <= max(Xrange) & Y >= min(Yrange) & Y <= max(Yrange);

    set(kids(k),'XDATA',X(idx),'YDATA',Y(idx));
end

如果你的绘图有补丁对象、条形对象等,你将不得不修改它,但想法就在那里。

边缘情况:

正如@Jan 正确指出的那样,有一些边缘情况需要考虑。第一个是,即使在扩展的尺度中,上述方法也假设点的密度相当高,并且会在线的末端和轴之间留下一个小间隙。对于低点密度线,您需要扩展 idx 变量以捕获任一方向轴外的下一个点:

idx_center = X >= min(Xrange) & X <= max(Xrange) & Y >= min(Yrange) & Y <= max(Yrange);
idx_plus = [false idx(1:end-1)];
idx_minus = [idx(2:end) false];

idx = idx_center | idx_plus | idx_minus;

如果窗口内至少有一个点,则只会扩大点数。另一种边缘情况是线穿过窗口但不包含窗口内的任何点,如果 idx 全部为假,就会出现这种情况。在这种情况下,您需要左轴外的最大点和右轴外的最小点。如果这些搜索中的任何一个为空,则该线不会通过窗口并可以被丢弃。

if ~any(idx)
   idx_low = find(X < min(Xrange),1,'last');
   idx_high = find(X > max(Xrange),1,'first');

   if isempty(idx_low) | isempty(idx_high)
       % if there aren't points outside either side of the axes then the line doesn't pass through
       idx = false(size(X));
   else
       idx = idx_low:idx_high;
   end
end

这不会测试线是否在 y 边界内,因此线可能会在窗口上方或下方通过而不相交。如果这很重要,您将需要测试连接找到的点的线。内存多2分应该没什么大不了的。如果这是一个问题,那么我会把它作为练习留给学生来测试一条线是否穿过坐标轴。

综合起来:

kids = findobj(gca,'Type','line'); % find children of the axes that are lines
K = length(kids); % count them

Xrange = get(gca,'XLim'); % get the axis limits
Yrange = get(gca,'YLim');

for k = 1:K
    X = get(kids(k),'XData'); % pull the x-y data for the line
    Y = get(kids(k),'YData');

    % find the points inside the window and then dilate the window by one in
    % each direction
    idx_center = X >= min(Xrange) & X <= max(Xrange) & Y >= min(Yrange) & Y <= max(Yrange);
    idx_plus = [false idx(1:end-1)];
    idx_minus = [idx(2:end) false];

    idx = idx_center | idx_plus | idx_minus;

    % handle the edge case where there are no points in the window but the line still passes through
    if ~any(idx)
       idx_low = find(X < min(Xrange),1,'last');
       idx_high = find(X > max(Xrange),1,'first');

       if isempty(idx_low) | isempty(idx_high)
           % if there aren't points outside either side of the axes then the line doesn't pass     
           % through
           idx = false(size(X));
       else
           % numerical indexing instead of logical, but it all works the same
           idx = idx_low:idx_high;
       end
    end

    if any(idx)
        % reset the line with just the desired data
        set(kids(k),'XDATA',X(idx),'YDATA',Y(idx));
    else
        % if there still aren't points in the window, remove the object from the figure
        delete(kids(k));
    end
end

关于matlab - 在 Matlab 中保存裁剪图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26109192/

相关文章:

python - 使用子图调整图形周围的空间

matlab - 显示新图像时如何使 Matlab 图形窗口最大化?

matlab - 从不规则数据绘制表面

MATLAB : import package for base class

matlab - 绘图叠加 MATLAB

facebook - 像 Flickr 和 Facebook 照片流一样将照片大小调整为缩略图方 block ?

python - 使用 Python 进行图像裁剪

python - 如何使用立体相机创建良好的深度图?

performance - Matlab 性能 : comparison slower than arithmetic

ffmpeg - 使用 FFMPEG 裁剪视频时出错