3
$\begingroup$

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], ""}, 
       AxesStyle -> Arrowheads[.04], 
       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"]

enter image description here

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?

$\endgroup$
  • $\begingroup$ Which version of Mathematica are you using? $\endgroup$ – C. E. May 30 '14 at 1:38
  • $\begingroup$ @Pickett,Version 8.0.4 $\endgroup$ – user8336 May 30 '14 at 1:49
  • 1
    $\begingroup$ 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]] $\endgroup$ – Dr. belisarius May 30 '14 at 2:52
  • 1
    $\begingroup$ 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}] $\endgroup$ – Jens May 30 '14 at 3:05
  • $\begingroup$ @Jens,Wow,Perfect! $\endgroup$ – user8336 May 30 '14 at 3:27
2
$\begingroup$

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

plotlegends

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.

$\endgroup$
1
$\begingroup$

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

enter image description here

$\endgroup$
1
$\begingroup$
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}}]
$\endgroup$
  • $\begingroup$ Thanks!I discovered that the plotrange of y axis will in valid in this solution. $\endgroup$ – user8336 May 30 '14 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy