Is it possible to orient the legend's symbols vertically instead of horizontally?

5 ビュー (過去 30 日間)
Sim
Sim 2025 年 3 月 27 日
編集済み: Benjamin Kraus 2025 年 3 月 31 日
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)');

採用された回答

DGM
DGM 2025 年 3 月 28 日
編集済み: DGM 2025 年 3 月 28 日
It can be done by manipulating undocumented properties, but there's a good chance it might cause problems, particularly when trying to save the figure. It's up to you to accept the risk of extra complications. That said, I was able to save the figure without it breaking (at least in R2019b).
% the example code
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);
% choose the legend orientation as needed
hl = legend([h1, h2], 'sin(x)', 'cos(x)', 'Orientation', 'vertical');
% rotate the legend icon lines so that they're vertical
drawnow % make sure everything is up to date
nlegicons = numel(hl.EntryContainer.Children);
for k = 1:nlegicons
% this vertex data is of the form [x0 x1; y0 y1; z0 z1],
% and are in normalized coordinates with respect to the little icon box area.
hl.EntryContainer.Children(k).Icon.Transform.Children.Children.VertexData = single([0.5 0.5; 0 1; 0 0]);
end
% resize the icon column
% like Les pointed out, this is canonically accessible in newer versions,
% but i'm doing it this way since i'm using R2019b
oldtokensize = hl.ItemTokenSize;
hl.ItemTokenSize = oldtokensize.*[0.3; 1]; % reduce the width
Given that the tokensize is typically much wider than tall, this may become problematic when trying to also represent a line+marker style, especially with a heavy lineweight. I'm sure the vertical size of the legend entries could be adjusted, but it would be an extra complication. It's not directly adjustable via the tokensize parameter at least. It likely requires manipulation of the parent box and the positions of the contained objects, but I haven't dug that far yet.
  3 件のコメント
Sim
Sim 2025 年 3 月 31 日
編集済み: Sim 2025 年 3 月 31 日
Thanks @DGM for your solution!
Thanks @Thomas Pursche for your comment, I will bear in mind it when I will change version :-)
DGM
DGM 2025 年 3 月 31 日
@Benjamin Kraus's answer may work if you're using a newer version than I am.

サインインしてコメントする。

その他の回答 (2 件)

Thomas Pursche
Thomas Pursche 2025 年 3 月 27 日
編集済み: Thomas Pursche 2025 年 3 月 27 日
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 件のコメント
Sim
Sim 2025 年 3 月 31 日
編集済み: Sim 2025 年 3 月 31 日
Thanks @Les Beckham :-)
Les Beckham
Les Beckham 2025 年 3 月 31 日
Of course!

サインインしてコメントする。


Benjamin Kraus
Benjamin Kraus 2025 年 3 月 31 日
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 件のコメント
DGM
DGM 2025 年 3 月 31 日
編集済み: DGM 2025 年 3 月 31 日
Well that was an unforeseen workaround. It's not a valid marker style in R2019b, but I think it was introduced in R2020b?
Benjamin Kraus
Benjamin Kraus 2025 年 3 月 31 日
編集済み: Benjamin Kraus 2025 年 3 月 31 日
That's correct. The vertical bar and horizontal bar markers were added in R2020b.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by