凡例の場所を 'Best' (のようにlegend('y1','y2','Location','Best')
) に設定して、凡例が私の行と衝突しないようにしたいのですが、同時に、データの衝突なしで可能であれば隅に置きたいと思います。これを実装する方法はありますか?
1116 次
2 に答える
5
誰かがこれに興味を持っている場合に備えて、私は @S.. answer に基づいて関数を書きました。それは私が達成したかったことです。コードは次のとおりです。
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
于 2016-01-20T09:12:35.107 に答える
2
完全な答えはありません。スケッチだけです。ただし、最初に凡例を隅に設定することを試みることができます
a=legend('y1', 'y2', 'Location', 'NorthEast')
そしてその位置を取得します
get(a,'Position')
この位置を座標に変換し、 http://www.mathworks.com/matlabcentral/fileexchange/11837-fast-and-robust-curve-intersectionsを使用して、線が凡例の境界をまたぐかどうかを簡単にテストできます 。コーナーがなくなるまで、別のコーナーを試してください。その場合は、「ベスト」を使用してください。
于 2016-01-05T17:21:07.440 に答える