除了@yuk 回答,如果你也有图例,
hline = findobj(gcf, 'type', 'line');
将返回 N x 3 行(或更准确地说 - lines plotted + 2x lines in legend)。 我这里只看所有绘制的线都在图例中的情况。
排序很奇怪:
如果绘制了 5 行(让我们记下它们1 to 5)并添加了图例,您将拥有
hline:
1 : 5 th line (mistical)
2 : 5 th line (in legend)
3 : 4 th line (mistical)
4 : 4 th line (in legend)
5 : 3 th line (mistical)
6 : 3 th line (in legend)
7 : 2 th line (mistical)
8 : 2 th line (in legend)
9 : 1 th line (mistical)
10: 1 th line (in legend)
11: 5 th line (in plot)
12: 4 th line (in plot)
13: 3 th line (in plot)
14: 2 th line (in plot)
15: 1 th line (in plot)
作为解决方案(周五晚上的拖延症)我做了这个小宝宝:
解决方案 1:如果您不想重置图例
检测是否有图例以及绘制了多少行:
hline = findobj(gcf, 'type', 'line');
isThereLegend=(~isempty(findobj(gcf,'Type','axes','Tag','legend')))
if(isThereLegend)
nLines=length(hline)/3
else
nLines=length(hline)
end
为每一行找到正确的句柄并为该行做一些事情(它也适用于相应的图例行)
for iterLine=1:nLines
mInd=nLines-iterLine+1
if(isThereLegend)
set(hline([(mInd*2-1) (mInd*2) (2*nLines+mInd)]),'LineWidth',iterLine)
else
set(hline(mInd),'LineWidth',iterLine)
end
end
这使得每个i-th 行与width=i 一致,您可以在此处添加自动属性更改;
解决方案 2:保持简单
摆脱图例,照顾线条,重置图例。
legend off
hline = findobj(gcf, 'type', 'line');
nLines=length(hline)
for iterLine=1:nLines
mInd=nLines-iterLine+1
set(hline(mInd),'LineWidth',iterLine)
end
legend show
这可能不适合必须将图例放置在某个特定位置等情况。