Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 图例位置“最佳”,但如果可能,仍在角落中_Matlab_Matlab Figure_Legend_Legend Properties - Fatal编程技术网

Matlab 图例位置“最佳”,但如果可能,仍在角落中

Matlab 图例位置“最佳”,但如果可能,仍在角落中,matlab,matlab-figure,legend,legend-properties,Matlab,Matlab Figure,Legend,Legend Properties,我想将图例的位置设置为“最佳”,如图例“y1”、“y2”、“Location”和“Best”,这样图例就不会与我的线条发生冲突,但同时,如果没有数据冲突,我更希望将其放在角落里。有什么方法可以实现这一点吗?我没有一个完整的答案,只有一个草图。但是,您可以尝试首先将图例设置在角落中 a=legend('y1', 'y2', 'Location', 'NorthEast') 然后得到它的位置 get(a,'Position') 您可以将此位置转换为坐标,并使用 .如果是这种情况,请尝试另一个拐角

我想将图例的位置设置为“最佳”,如图例“y1”、“y2”、“Location”和“Best”,这样图例就不会与我的线条发生冲突,但同时,如果没有数据冲突,我更希望将其放在角落里。有什么方法可以实现这一点吗?

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

a=legend('y1', 'y2', 'Location', 'NorthEast')
然后得到它的位置

get(a,'Position')
您可以将此位置转换为坐标,并使用
.如果是这种情况,请尝试另一个拐角,直到没有拐角为止。在这种情况下,请使用“Best”。

如果有人对此感兴趣,我将基于@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

听起来不错。我会试试这个,尽快告诉你是否有效。不过,这可能需要几天时间。到目前为止,感谢您的努力!等等,我怎么把图例的位置转换成绘图坐标中的曲线呢?这有点烦人,但是你可以得到x轴和y轴的位置,你也知道它们的范围。所以你可以用它来转换坐标中的任何位置。我同意这个解决方案很难看,但它可能会起作用。问题:它对对数图也有效吗?@AndrasDeak如果答案是“不”,我看不出这个问题的诀窍:DOK,它可能只是一个修辞问题;比如:我没有尝试。你的剧本很棒。对我很有用。但是,我应该报告一个小错误,当绘图限制不是默认限制时,当它们与数据长度不一致时,会发生此错误。有必要将表达式“x1、x3”、“y1”、“y3”更正为考虑到这一点。它变成如下:x1=LimX1+legPos1-axPos1*xScaling;x3=LimX1+legPos1+legPos3-axPos1*xScaling;y1=LimY1+legPos2-axPos2*yScaling;y2=LimY1+legPos2+legPos4-axPos2*yScaling@JustinoRodrigues感谢您的积极反馈。你完全正确,谢谢你的提示。