# How to rotate legend in matlab 2019

조회 수: 28 (최근 30일)
CHEN Jianghua 2020년 6월 3일
댓글: yuxuan 2023년 12월 24일
Hi, I would like to rotate the legend from horizontal layout to vertical layout, just as shown in the picture below. Could anyone share some experience about this?
Thanks for you coming help!
Alden
##### 댓글 수: 2없음 표시없음 숨기기
Walter Roberson 2020년 6월 3일
CHEN Jianghua 2020년 6월 3일
Thank you so much. I will keep an eye on that.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Sai Sri Pathuri 2020년 7월 10일
There is no direct function to rotate legends in graphs using MATLAB.
However, as a workaround we can manually rotate the legends. Please refer to the following example:
% create a sample plot
h1 = plot(1:5);
% add legend to the graph
[legend_handle, icons] = legend('Sample legend');
% disable the box around the legend object
set(legend_handle, 'Box', 'off')
% get the current position of the legend object
leg_pos=get(legend_handle,'position');
% assign the required position of the legend to a new variable
new_leg_pos=[.7 0.6 .2 leg_pos(4)+.2] ;
% Get current line data (horizontal line)
xd = icons(2).XData;
yd = icons(2).YData;
% Swap X and Y data for line (make vertical line)
icons(2).XData = yd;
icons(2).YData = xd;
% Rotate and reposition the text
set(icons(1),'rotation',90)
icons(1).Position = [0.5 0.4 0];
% Adjust legend size to accomodate changes.
set(legend_handle,'position',new_leg_pos);
##### 댓글 수: 5이전 댓글 3개 표시이전 댓글 3개 숨기기
Joan Sturm 2021년 5월 3일
I do have the same problem and I'm working with R2020a.
yuxuan 2023년 12월 24일
This only works on the plot figure, when i use the 'errorbar' function same error shows up.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by