필터 지우기
필터 지우기

How do i rotate legend in plot

조회 수: 53 (최근 30일)
Derek Cooper
Derek Cooper 2021년 12월 20일
답변: Ashutosh Singh Baghel 2021년 12월 22일
Hello dear friends,
I would like to rotate my legend box in a plot 90 degrees, like the figure below.

답변 (1개)

Ashutosh Singh Baghel
Ashutosh Singh Baghel 2021년 12월 22일
Hi Derek,
I understand you wish to rotate the legend by 90 degrees. However, there is no direct function to rotate legends in graphs using MATLAB. As a workaround we can manually rotate the legends. Please refer to the following example:
f = 50;
t = 0:1/51:2;
z1 = sin(2*pi*1*f*t);
h1 = plot(t,z1);
% create a sample plot
% 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);
Refer to MATLAB Documentation for 'legend' for more information.

카테고리

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

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by