# Adding plot legends in version 8

I use Mathematica to draw the displacement, velocity, and acceleration of one point in a graphic.

FinalGraphics[Joint_, plotRange_List, opt_String] :=
Module[{JointDisplacement, JointVelocity, JointAccelaration},
JointDisplacement = Plot[Joint, {t, plotRange[[1, 1]], plotRange[[1, 2]]}];
JointVelocity =
Plot[Evaluate @ D[Joint, t], {t, 0, 5}, PlotStyle -> {Black, Dashed}];
JointAccelaration =
Plot[Evaluate @ D[Joint, {t, 2}], {t, 0, 5}, PlotStyle -> {Thick, Orange, Dashed}];
Column[{
Show[{JointDisplacement, JointVelocity, JointAccelaration},
AxesLabel -> {Style["t(s)", 15], ""},
PlotRange -> plotRange,
ImageSize -> 450,
GridLines -> {None, Automatic}], opt},
Center]]


Now I use my function:

Joint1 =
-0.609735 + 0.0436332 t^2 + 0.0327806 t^3 - 0.0133248 t^4 + 0.00120561 t^5;
FinalGraphics[Joint1, {{0, 5}, {-.6, .5}}, "Graphic5.1  the displacement, velocity, acceleration of joint1"]


However, the above graphic lacks plotlegends, so I rewrite my code:

Needs["PlotLegends"]
FinalGraphicsLegend[Joint_, plotRange_List] :=
Module[{},
Plot[{Joint, Evaluate @ D[Joint, t], Evaluate @ D[Joint, {t, 2}]},
{t, plotRange[[1, 1]], plotRange[[1, 2]],
PlotLegend -> {"displacement", "velocity", "acceleration"}}]]


but it fails.

FinalGraphicsLegend[Joint1, {{0, 5}, {-.6, .5}}]


General::ivar: 0.00010214285714285715 is not a valid variable. >>

So my question is:

How do I correct the function FinalGraphicsLegend or add plot legends to the function FinalGraphic in some other way?

• Which version of Mathematica are you using? Commented May 30, 2014 at 1:38
• @Pickett,Version 8.0.4
– user8336
Commented May 30, 2014 at 1:49
• Haven't checked the whole thing, but a quick fix: FinalGraphicsLegend[Joint_, plotRange_List] := Module[{}, Plot[{Joint, Evaluate@D[Joint, t], Evaluate@D[Joint, {t, 2}]}, {t, plotRange[[1, 1]], plotRange[[1, 2]]}, PlotLegend -> {"displacement", "velocity", "acceleration"}, Evaluated -> True]] Commented May 30, 2014 at 2:52
• It also works if you use the autoLegend function defined in the first code block of my answer here and write the Column like this: Column[{Show[autoLegend[Show[{JointDisplacement, JointVelocity, JointAccelaration}, AxesLabel -> {Style["t(s)", 15], ""}, AxesStyle -> Arrowheads[.04], PlotRange -> plotRange, GridLines -> {None, Automatic}], {"displacement", "velocity", "acceleration"}], ImageSize -> 450], opt}]
– Jens
Commented May 30, 2014 at 3:05
• @Jens,Wow,Perfect!
– user8336
Commented May 30, 2014 at 3:27

As I said in the comments, a solution that was originally designed for version 8 is my answer here. After executing the definitions in your question and in the first code block of my answer, you just rewrite the plotting function as follows:

FinalGraphics[Joint_, plotRange_List, opt_String] :=
Module[{JointDisplacement, JointVelocity, JointAccelaration},
JointDisplacement =
Plot[Joint, {t, plotRange[[1, 1]], plotRange[[1, 2]]}];
JointVelocity =
Plot[Evaluate@D[Joint, t], {t, 0, 5}, PlotStyle -> {Black, Dashed}];
JointAccelaration =
Plot[Evaluate@D[Joint, {t, 2}], {t, 0, 5},
PlotStyle -> {Thick, Orange, Dashed}];
Column[
{
Show[
autoLegend[
Show[{JointDisplacement, JointVelocity, JointAccelaration},
AxesLabel -> {Style["t(s)", 15], ""},
AxesStyle -> Arrowheads[.04], PlotRange -> plotRange,
GridLines -> {None, Automatic}], {"displacement", "velocity",
"acceleration"}, Alignment -> {Right, Bottom}
], ImageSize -> 450
], opt
}
]]

FinalGraphics[Joint1, {{0,
5}, {-.6, .5}}, "Graphic5.1  the displacement,velocity,accelaration of joint1"]


Here I set the legend to appear at the bottom right by adding the option Alignment -> {Right, Bottom} to autoLegend. The autoLegend command wraps the contents of the first row of your Column. In addition, I then wrap that output of autoLegend in another Show to adjust the ImageSize.

Add the legends to each plot as follows:

FinalGraphics[Joint_, plotRange_List, opt_String] :=
Module[{JointDisplacement, JointVelocity, JointAccelaration},
JointDisplacement =
Plot[Joint, {t, plotRange[[1, 1]], plotRange[[1, 2]]},
PlotLegends -> {"displacement"}];
JointVelocity =
Plot[Evaluate@D[Joint, t], {t, 0, 5}, PlotStyle -> {Black, Dashed},
PlotLegends -> {"velocity"}];
JointAccelaration =
Plot[Evaluate@D[Joint, {t, 2}], {t, 0, 5},
PlotStyle -> {Thick, Orange, Dashed},
PlotLegends -> {"acceleration"}];
Column[{Show[{JointDisplacement, JointVelocity, JointAccelaration},
AxesLabel -> {Style["t(s)", 15], ""},
AxesStyle -> Arrowheads[.04], PlotRange -> plotRange,
ImageSize -> 450, GridLines -> {None, Automatic}], opt}, Center]]

Joint1 = -0.609735 + 0.0436332 t^2 + 0.0327806 t^3 - 0.0133248 t^4 +
0.00120561 t^5; FinalGraphics[Joint1, {{0,
5}, {-.6, .5}}, "Graphic5.1 the displacement,velocity,accelaration \
of joint1"]


Joint1 = -0.609735 + 0.0436332 t^2 + 0.0327806 t^3 - 0.0133248 t^4 + 0.00120561 t^5;

Needs["PlotLegends"]

FinalGraphicsLegend[Joint_, plotRange_List] :=
Plot[Evaluate[NestList[D[#, t] &, Joint, 2]], {t, plotRange[[1, 1]], plotRange[[1, 2]]},
PlotLegend -> {"displacement", "velocity", "acceleration"},
LegendPosition -> {0.25, -0.5},
LegendSize -> {.6, .35}]

FinalGraphicsLegend[Joint1, {{0, 5}, {-.6, .5}}]

• Thanks!I discovered that the plotrange of y axis` will in valid in this solution.
– user8336
Commented May 30, 2014 at 3:57