Legend with certain number of rows and columns

79 views (last 30 days)
Hello everyone,
I would like to plot 6 curves in a diagram and the legend should have the first 4 curves in the first column and the last two curves in the second column. How can I arrange this? Thank you very much for your help!
  1 Comment
Askic V
Askic V on 25 Jan 2023
Davide provided the answer. Just in case you're interested to learn more about different options, similar question was answered by Mathworks Support Team here:

Sign in to comment.

Accepted Answer

Davide Masiello
Davide Masiello on 25 Jan 2023
Edited: Davide Masiello on 25 Jan 2023
Use legendflex from file exchange.
x = linspace(0,1,1000);
n = (1:6)';
y = x.^n;
p = plot(x,y);
title('y = x^n')
'ncol',2,'nrow',4,'anchor',[1 1],'buffer',[10 -10],'box','off')
Davide Masiello
Davide Masiello on 25 Jan 2023
I guess this could be considered cheating, but it does work
x = linspace(0,1,1000);
n = (1:6)';
y = x.^n;
p1 = plot(x,y(1,:)); hold on
p2 = plot(x,y(2,:));
p3 = plot(x,y(3,:));
p4 = plot(x,y(4,:));
p5 = plot(x,y(5,:));
p6 = plot(x,y(6,:));
legendflex(p1,{'n=1'},'anchor',[1 1],'buffer',[10 -10],'box','off')
legendflex(p2,{'n=2'},'anchor',[1 1],'buffer',[10 -25],'box','off')
legendflex(p3,{'n=3'},'anchor',[1 1],'buffer',[10 -40],'box','off')
legendflex(p4,{'n=4'},'anchor',[1 1],'buffer',[10 -55],'box','off')
legendflex(p5,{'n=5'},'anchor',[1 1],'buffer',[80 -10],'box','off')
legendflex(p6,{'n=6'},'anchor',[1 1],'buffer',[80 -25],'box','off')

Sign in to comment.

More Answers (0)


Find more on 2-D and 3-D Plots 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!