传奇位置'最佳',但如果可能仍然在角落里

时间:2016-01-04 14:47:13

标签: matlab matlab-figure legend legend-properties

我想将我的传奇的位置设置为' Best' (比如legend('y1','y2','Location','Best'))因此传说不会与我的线条发生碰撞,但与此同时,如果可能没有数据冲突,我宁愿将它放在角落里。有没有办法实现这个?

2 个答案:

答案 0 :(得分:5)

如果有人对此感兴趣,我写了一个基于@S的函数..答案就是这样,我想要实现的。这是代码:

function setPositionCornerBest( figureHandle )
%Sets the Location of the legend of the figure that is referenced by figureHandle to one of the Corners if there is no data in the Corners. Otherwise it sets it to 'Best'
h = figureHandle;

figObjects = get(h,'Children');
legHandle = findobj(figObjects,'Tag','legend');
axHandle = findobj(figObjects,'Type','axes','-and','Tag','');
lineHandle = findobj(figObjects,'Type','line','-and','Parent',axHandle);
axPos = get(axHandle,'Position');

LimX = get(axHandle,'XLim');
LimY = get(axHandle,'YLim');

xScaling = (LimX(2)-LimX(1))/axPos(3);
yScaling = (LimY(2)-LimY(1))/axPos(4);

locCell = {'NorthWest','NorthEast','SouthEast','SouthWest'};
ii = 1;
interSecFlag = true;

while (interSecFlag) && (ii<=4)
    set(legHandle,'Location',locCell{ii});
    legPos = get(legHandle,'Position');

    x(1) = LimX(1)+(legPos(1)-axPos(1))*xScaling;
    x(2) = x(1);
    x(3) = LimX(1)+(legPos(1)+legPos(3)-axPos(1))*xScaling;
    x(4) = x(3);
    x(5) = x(1);

    y(1) = LimY(1)+(legPos(2)-axPos(2))*yScaling;
    y(2) = LimY(1)+(legPos(2)+legPos(4)-axPos(2))*yScaling;
    y(3) = y(2);
    y(4) = y(1);
    y(5) = y(1);

    for jj = 1:numel(lineHandle)
        xline = get(lineHandle(jj),'XData');
        yline = get(lineHandle(jj),'YData');
        [xInter ~] = intersections(x,y,xline,yline);
        if numel(xInter) == 0
            xInterFlag(jj) = 0;
        else
            xInterFlag(jj) = 1;
        end
    end

    if all(xInterFlag==0)
        interSecFlag = false;
    end

    ii = ii + 1;
end

if interSecFlag
    set(legHandle,'Location','Best');
end

end

答案 1 :(得分:2)

我没有完整的答案,只有草图。但是,您可以尝试首先在角落设置图例

a=legend('y1', 'y2', 'Location', 'NorthEast')

然后获得它的位置

get(a,'Position')

您可以将此位置转换为坐标,并简单地测试您的线条是否跨越图例的任何边框 http://www.mathworks.com/matlabcentral/fileexchange/11837-fast-and-robust-curve-intersections 如果是这种情况,请尝试另一个角落,直到没有角落为止。在这种情况下,请使用“最佳”。