Is it possible to orient the legend's symbols vertically instead of horizontally?
Show older comments
Is it possible to orient the legend's symbols vertically instead of horizontally?
In the following example, the red and blue lines in the legend are horizontally oriented, but I would like them to be vertically oriented within the legend:
x = linspace(0, 10, 100);
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'red', x, y2, 'blue');
hold on;
h1 = plot(nan, nan, 'red', 'LineWidth', 2);
h2 = plot(nan, nan, 'blue', 'LineWidth', 2);
legend([h1, h2], 'sin(x)', 'cos(x)');
Accepted Answer
More Answers (2)
Thomas Pursche
on 27 Mar 2025
Edited: Thomas Pursche
on 27 Mar 2025
0 votes
Hi,
yes it is possible. Just change
legend([h1, h2], 'sin(x)', 'cos(x)');
to
legend([h1, h2], 'sin(x)', 'cos(x)','Orientation','vertical');
by adding the property 'Location', you also can edit the position of the legend. For example: 'Location','southwest'
Best regards,
Thomas
3 Comments
The 'Orientation' option for a legend doesn't set the orientation of the symbols in the legend, it sets the arrangement of the legend entries. The default is 'vertical', so the suggestion in this answer won't affect the appearance of the legend at all. See below for an example showing the effect of 'horizontal' orientation.
I know of no way to achieve what OP is looking for. Since 2024a, it is possible to control the size (width) of the symbols using the IconColumnWidth property. The example below also illustrates this, making the lines shorter.
x = linspace(0, 10, 100);
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'red', x, y2, 'blue');
grid on
hold on;
h1 = plot(nan, nan, 'red', 'LineWidth', 2);
h2 = plot(nan, nan, 'blue', 'LineWidth', 2);
legend([h1, h2], 'sin(x)', 'cos(x)', 'Orientation', 'horizontal', 'IconColumnWidth', 10);
Les Beckham
on 31 Mar 2025
Of course!
Does this work for you? This uses only fully documented features.
x = linspace(0, 10, 100);
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'red', x, y2, 'blue');
hold on;
h1 = plot(nan, nan, 'red', 'LineWidth', 2, 'Marker', '|', 'LineStyle','none');
h2 = plot(nan, nan, 'blue', 'LineWidth', 2, 'Marker', '|', 'LineStyle','none');
legend([h1, h2], 'sin(x)', 'cos(x)');
2 Comments
Benjamin Kraus
on 31 Mar 2025
Edited: Benjamin Kraus
on 31 Mar 2025
That's correct. The vertical bar and horizontal bar markers were added in R2020b.
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


