## Adding plot legends in version 8

3

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? – C. E. – 2014-05-30T01:38:32.337

@Pickett,Version 8.0.4 – None – 2014-05-30T01:49:54.130

1Haven'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]] – Dr. belisarius – 2014-05-30T02:52:48.933

1

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 – 2014-05-30T03:05:35.930

@Jens,Wow,Perfect! – None – 2014-05-30T03:27:03.577

@tangshutao Glad you like it - I added the tag for version 8 to this question to make it clearer that you can't solve it by using version 9 commands. – Jens – 2014-05-30T03:31:59.430

@belisarius,thanks for your help.However,the PlotRange becomes invalid – None – 2014-05-30T03:39:31.367

2

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.

1

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"]


1

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. – None – 2014-05-30T03:57:04.943