How to rotate legend in matlab 2019

C
CHENJianghua · Dec 28, 2023 · 1.9K views
Question
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!  
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Sep 17, 2026





   

Rotating Legends in MATLAB R2019

   

MATLAB R2019 (both R2019a and R2019b) handles legend rotation in two different ways depending on what you want to achieve:


   

           
  1. Switching the overall legend layout between horizontal and vertical.

  2.        
  3. Rotating the actual text labels inside the plot (such as angled or vertical text strings).

  4.    

   

Method 1: Change Overall Legend Orientation (Horizontal vs. Vertical)


   

The standard legend object includes an Orientation property. This changes whether the entries stack in a column or spread across a row.

   

% Sample data
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);

% Plot curves
figure;
plot(x, y1, 'b-', 'LineWidth', 1.5);
hold on;
plot(x, y2, 'r--', 'LineWidth', 1.5);

% Create and rotate legend layout to horizontal
lgd = legend('Sine Wave', 'Cosine Wave');
lgd.Orientation = 'horizontal';
lgd.Location = 'northoutside';

   


        In MATLAB R2019b: You can also set lgd.NumColumns = 2; to control exact column wrapping.
   

   

Method 2: Rotate Text Labels (Angled Text)


   

The standard MATLAB legend object does not have a native angle property (like 45° or 90°) for its internal labels. If you need angled or rotated text labels next to your lines, construct custom text annotations using the text command with the 'Rotation' property.

   

x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);

figure;
plot(x, y1, 'b', x, y2, 'r', 'LineWidth', 1.5);
grid on;

% Add custom rotated labels directly along curves
text(x(30), y1(30), '  \leftarrow Sine (Rotated 45^\circ)', ...
    'Rotation', 45, 'FontSize', 10, 'Color', 'b', 'FontWeight', 'bold');

text(x(70), y2(70), '  \leftarrow Cosine (Rotated -30^\circ)', ...
    'Rotation', -30, 'FontSize', 10, 'Color', 'r', 'FontWeight', 'bold');

   

Method 3: Build a Custom Rotated Legend Box


   

To place an independent box with 90-degree rotated labels, overlay an invisible secondary axis or use text inside a dedicated bounding box.

   

x = linspace(0, 10, 100);
figure;
h1 = plot(x, sin(x), 'b'); hold on;
h2 = plot(x, cos(x), 'r');

% Add a rotated text annotation block
txt = sprintf('Line 1: sin(x)\nLine 2: cos(x)');
t = text(8.5, 0.5, txt, 'Rotation', 90, ...
    'EdgeColor', [0.8 0.8 0.8], ...
    'BackgroundColor', [1 1 1], ...
    'Margin', 8, ...
    'FontSize', 10);

   

Summary Comparison


   

           
  • Horizontal row vs. vertical column: Use lgd.Orientation = 'horizontal' or lgd.Orientation = 'vertical'.

  •        
  • Specific angular rotation (e.g., 45° or 90°): Use the text() function with the 'Rotation', angle parameter.

  •    


100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!