如何删除3D matlab图的背面

时间:2017-02-01 23:45:46

标签: matlab plot

在matlab中绘制3d图形时,边界框的背面填充为白色:

enter image description here

使用

可以轻松删除所有内容
ax = gca;
ax.Color = [0, 0, 0, 0];

如何只拆除后侧? (除了地板之外的一切)

1 个答案:

答案 0 :(得分:2)

使用未记录的Axes.Backdrop属性,您可以获得此行为。 Axes.Backdrop.Face.VertexData包含背景顶点列表。我们可以找到并保持最佳状态:

ax = gca;
face = ax.Backdrop.Face;

% can be replaced with conditions on other axes and limits
point_on_face = face.VertexData(3,:) == ax.ZLim(1);
is_target_face = all(reshape(point_on_face, 4, []));
target_face_verts = logical(kron(is_target_face, ones(1, 4)));

% discard all but the first quad (the floor)
face.VertexData = face.VertexData(:,target_face_verts);

但是,当轴旋转时,这些四边形会被重新绘制,这不是一个有效的解决方案。

我们可以通过添加事件监听器来进一步:

function h = set_walls(ax, varargin)
  function update()
    face = ax.Backdrop.Face;
    data = face.VertexData;
    if empty(data); return; end
    keep_verts = false(1, size(data, 2));
    for side = varargin'
      side = side{:};
      switch side
        case 'xmin'; point_on_face = data(1,:) == ax.XLim(1);
        case 'xmax'; point_on_face = data(1,:) == ax.XLim(2);
        case 'ymin'; point_on_face = data(2,:) == ax.YLim(1);
        case 'ymax'; point_on_face = data(2,:) == ax.YLim(2);
        case 'zmin'; point_on_face = data(3,:) == ax.ZLim(1);
        case 'zmax'; point_on_face = data(3,:) == ax.ZLim(2);
        otherwise; error('Unknown face');
      end
      is_target_face = all(reshape(point_on_face, 4, []));
      keep_verts = keep_verts | logical(kron(is_target_face, ones(1, 4)));
    end
    face.VertexData = data(:,keep_verts);
  end
  h = addlistener(ax, 'MarkedClean', @(x, y) update);
end

这闪烁,但它有效。该函数可用作set_walls(gca, 'zmin')

with removed background