# trouble with adding a legend to a plot

I am having trouble with adding a legend to a plot. this is my code,I tryed to use serval things,like

PlotLegends -> Automatic
PlotLegends -> {cVals[i]}
PlotLegends -> {i}
PlotLegends -> {cVals}


but they didn't give me what I hope!

what I want is writing the values of c in the plot,for example the red curve c=2.4 and the yellow curve c=2.2 ...etc

 Clear[t]
\[Tau] = 13.8;
\[Omega]0 = 1;
r = 0.7071;
n = 1.7758;
\[HBar] = 1.05457173*10^-34;
\[Omega] = 0.5;
k = 1666666.667;
s = 2.2758;
cVals = {2.4, 2.2, 2, 1.8, 1.5, 1.3};
color = {Red, Yellow, Green, Cyan, Blue, Orange};

Do[c = cVals[[i]];
system1 = {x'[t] == n*y[t], y'[t] == -n*x[t] - c*z[t],
z'[t] == c*y[t]};
initialvalues1 = {x[-20] == 0, y[-20] == 0, z[-20] == -1};
sol1 = NDSolve[
Join[system1, initialvalues1], {x[t], y[t], z[t]}, {t, -20, 20}];

R = (-c*r*y[t]);

Subscript[plotR, i] =
Plot[Evaluate[R /. sol1], {t, -20, 20}, FrameLabel -> {"t", "R"},
Frame -> True, FrameTicks -> All, PlotStyle -> color[[i]],
PlotLegends -> {cVals[i]}];
, {i, Length[cVals]}]

Show[{Subscript[plotR, 1], Subscript[plotR, 2], Subscript[plotR, 3],
Subscript[plotR, 4], Subscript[plotR, 5]}]


Clear[t]
\[Tau] = 13.8;
\[Omega]0 = 1;
r = 0.7071;
n = 1.7758;
\[HBar] = 1.05457173*10^-34;
\[Omega] = 0.5;
k = 1666666.667;
s = 2.2758;
cVals = {2.4, 2.2, 2, 1.8, 1.5, 1.3};
color = {Red, Yellow, Green, Cyan, Blue, Orange};

Do[c = cVals[[i]];
system1 = {x'[t] == n*y[t],
y'[t] == -n*x[t] - c*z[t],
z'[t] == c*y[t]};
initialvalues1 = {x[-20] == 0, y[-20] == 0, z[-20] == -1};
sol1 = NDSolve[
Join[system1, initialvalues1], {x[t], y[t], z[t]}, {t, -20, 20}];
R = (-c*r*y[t]);
Subscript[plotR, i] =
Plot[Evaluate[R /. sol1], {t, -20, 20},
FrameLabel -> {"t", "R"},
Frame -> True,
FrameTicks -> All,
PlotStyle -> color[[i]],
PlotLegends -> {Placed[
Text[Style[" " <> ToString[cVals[[i]]] <> " ", Bold,
color[[i]], Background -> White]], {Center, Bottom}]}];,
{i, Length[cVals]}
]

Show[{Subscript[plotR, 1], Subscript[plotR, 2], Subscript[plotR, 3], Subscript[plotR, 4], Subscript[plotR, 5]}]


There might be a missing double square bracket (you want the $i$th part of the list cVals instead of calling a function with argument $i$), i.e. instead of

cVals[i]


use

cVals[[i]]


Replacing the following in the definition of Subscript in your code gives a rudimentary legend:

PlotLegends -> {"c=" <> ToString[cVals[[i]]]}

• Work great! Thanks. Commented Jan 2, 2015 at 22:40