How to plot multiple lines using error bar, AND plot them with legends
12 views (last 30 days)
Show older comments
figure(1)
x=[1,2,3,4,5];
y1=[10,20,30,40,50];
y2=[12,22,32,42,52];
y3=[13,15,16,17,18];
y1err=[0.1,0.2,0.3,0.4,0.5];
y2err=[0.2,0.3,0.4,0.5,0.6];
hold on;
p1=errorbar(x,y1,y1err,'-om');
p2=errorbar(x,y2,y2err,'-ob');
p3=plot(x,y3);
xlabel('x','Interpreter','Latex')
ylabel('y','interpreter','latex')
hold off
legend([p1],[p2],[p3],{'$R_{MP}=60R_J$'},{'$R_{MP}=90R_J$'},{'$Dipole$'},'Interpreter','Latex')
%legend([p1],{$'leg1'$},'Interpreter','Latex')
%legend([p2],{'$leg2$'},'Interpreter','Latex')
%legend([p3],{'$leg3$'},'Interpreter','Latex')
I have also tried using the legend configuration which is % out. But only the legend for the [p3] plot shows up.
Thanks in advance!
0 Comments
Answers (2)
Peter Meglis
on 27 Jul 2018
Mike,
Your example code was really close. There were also a couple syntax errors but I think this is the legend that you intended.
...
y1err=[0.1,0.2,0.3,0.4,0.5]
...
p1=errorbar(x,y1,y1err,'-om')
...
legend({'$R_{MP}=60R_J$', '$R_{MP}=90R_J$', '$Dipole$'},'Interpreter','Latex')
https://www.mathworks.com/help/matlab/ref/legend.html
"legend(_,Name,Value) sets legend properties using one or more name-value pair arguments. When setting properties, you must specify the labels using a cell array, such as legend({'A','B'},'FontSize',12)."
0 Comments
dpb
on 27 Jul 2018
Edited: dpb
on 27 Jul 2018
Syntax error in the LaTeX strings...it doesn't like the cellstr inputs; use straight character strings if separate elements are passed.
legend('$R_{MP}=60R_J$','$R_{MP}=90R_J$','$Dipole$','Interpreter','Latex')
works here altho there's a warning 'Ignoring extra legend entries' that otomh...hmmm, I wonder? 'Interpereter' isn't allowed as named parameter pair here even though it's documented; I don't see the reason for that, but I note the tab completion only shows up 'Orientation' and 'Location' at that point. Looks like bug on that part, maybe??? Or at least it's inconsistent with documentation unless there's something about using the fancy characters that interacts with the input parsing.
legend('$R_{MP}=60R_J$','$R_{MP}=90R_J$','$Dipole$')
seems to work with no warnings (R2017b)
ADDENDUM
Forgot to note that per documentation, to use cellstrings, it's an array of 'em, not a list...
legend('{$R_{MP}=60R_J$','$R_{MP}=90R_J$','$Dipole$'},'Interpreter','Latex')
works including the named parameter with no warnings. Seems to be some input parsing problems with some input syntax forms that are documented to work.
0 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!