![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176177/image.png)
Legend horizontal orientation doesn't work when extracting icon handles
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi! I'm using Matlab 2015b and am trying to make a nice plot with a horizontal legend at the bottom. When I do like below, I get nice results (see figure below).
legendHandle = legend('1st' '2nd' '3rd' '4th' '5th' '6th');
legendHandle.Units = 'centimeters';
legendHandle.Orientation = 'horizontal';
legendHandle.Location = 'southOutside';
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/159164/image.png)
However, I want to reduce the legend sample line length, so I take out the handle to the icon objects as well.
[legendHandle,icons,~,~] = legend('1st' '2nd' '3rd' '4th' '5th' '6th');
legendHandle.Units = 'centimeters';
legendHandle.Orientation = 'horizontal';
legendHandle.Location = 'southOutside';
lineSamples = findall(icons,'Type','Line')
for i=1:numel(allSamples)
try
lineSamples(i).XData = [lineSamples(i).XData(1) lineSamples(i).XData(2)*0.5];
end
end
This yields a messed up result however (figure below), where the legends are not horizontal but still vertical but squished together.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/159165/image.png)
Can anyone point me in the right direction how to fix this? Thank you! /Ida
댓글 수: 0
채택된 답변
Massimo Zanetti
2016년 12월 21일
As far as I know the syntax [lgd,icons,plots,txt] = legend(..) is not reccomended. Notice in fact that by simply calling icons as output you destroy the legend layout:
%plot 3 random lines
plot(rand(10,3));
[lh,icons,~,~] = legend('1st','2nd','3rd');
lh.Units = 'centimeters';
lh.Orientation = 'horizontal';
lh.Location = 'southOutside';
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176177/image.png)
Therefore, after calling icons you need to replace ALL the objects in the legend. To do so, consider that icons objects of type 'Line' are twice the number of lines (in this case 6)
ls= findall(icons,'Type','Line')
ls =
6×1 Line array:
Line (1st)
Line (1st)
Line (2nd)
Line (2nd)
Line (3rd)
Line (3rd)
For every line that you see in the legend, you have 2 line objects! In parentheses the corresponding item label is reported by Matlab. For each line, the first object associated to it is the line object which is plotted in the legend, while the second one is its central position (inspect both XData and YData of all ls(k) for k=1,...,6). Thus, to make your custom legend, change all the ls lines XData and YData accordingly (for example, YData must be all the same while XData must be properly spaced). Remember, you also have to adjust text objects position.
My suggestion is to set 'Units' to 'normalized'.
Let me know if you need help.
댓글 수: 4
Massimo Zanetti
2016년 12월 22일
편집: Massimo Zanetti
2016년 12월 22일
Yes, by now there is no other way than re-adjusting all line and text objects one by one.. :(
However, Matlab indeed does a good job in arranging the objects.. do you really need to change the feel?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!