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

7 vues (au cours des 30 derniers jours)
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)');

Réponse acceptée

DGM
DGM le 28 Mar 2025
Modifié(e) : DGM le 28 Mar 2025
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 commentaires
Sim
Sim le 31 Mar 2025
Modifié(e) : Sim le 31 Mar 2025
Thanks @DGM for your solution!
Thanks @Thomas Pursche for your comment, I will bear in mind it when I will change version :-)
DGM
DGM le 31 Mar 2025
@Benjamin Kraus's answer may work if you're using a newer version than I am.

Connectez-vous pour commenter.

Plus de réponses (2)

Thomas Pursche
Thomas Pursche le 27 Mar 2025
Modifié(e) : Thomas Pursche le 27 Mar 2025
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 commentaires
Sim
Sim le 31 Mar 2025
Modifié(e) : Sim le 31 Mar 2025
Thanks @Les Beckham :-)

Connectez-vous pour commenter.


Benjamin Kraus
Benjamin Kraus le 31 Mar 2025
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 commentaires
DGM
DGM le 31 Mar 2025
Modifié(e) : DGM le 31 Mar 2025
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 le 31 Mar 2025
Modifié(e) : Benjamin Kraus le 31 Mar 2025
That's correct. The vertical bar and horizontal bar markers were added in R2020b.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by