2
$\begingroup$

I wrote the following code for create an animation with legend: however I was not able to put the LineLegend command into the command Animate and so it seems that this is the reason for which I can't export the animation in a gif-animation. So I'd like know how create a legend for an animation and how put it in a specific position and so finally how export it: I point out that I have to export a row animation if possible. So to follow the code

Subscript[x, 0][j_] := 1 (*Ascissa iniziale del j-esimo punto*)
Subscript[y, 0][j_] := j(*Ordinata iniziale del j-esimo punto*)
x[t_, j_] := 
 Subscript[x, 0][
   j] (t^2 + 
    1)(*Legge orraria del j-esimo punto sull'asse delle ascisse*)
y[t_, j_] := 
 Subscript[y, 0][
   j] (t + 1)(*Legge oraria del j-esimo punto sull'asse delle \
ordinate*)
T[t_, j_] := {x[t, j], y[t, j]}(*Traiettoria del j-esimo punto*)
Color = {Red, Green, Blue};(*Lista colori*)
Trajectory[j_, i_, r_, g_, b_, o_, s_] := 
 Piecewise[{{ParametricPlot[T[t, j], {t, 0, i}, ImageSize -> Large, 
     AspectRatio -> 1, PlotStyle -> Extract[Color, j]], 
    s == 0}, {ParametricPlot[T[t, j], {t, 0, i}, ImageSize -> Large, 
     AspectRatio -> 1, 
     PlotStyle -> {RGBColor[r, g, b], Directive[Opacity[o]]}], 
    s == 1}}]
P[t_, j_] := {PointSize[0.01], Point[T[t, j]]}(*Punto j-esimo*)
V[t_, j_] = {Arrowheads[0.03], 
   Arrow[{T[t, j], 
     T[t, j] + {D[x[t, j], t], 
       D[y[t, j], 
        t]}}]};(*Vettore velocità della j-esima traiettoria al tempo \
t*)
MotionPoints[n_, i_] := 
  Legended[Animate[
    Show[Table[Trajectory[j, i, 0, 0, 0, 0, 0], {j, n}], 
     Graphics[Table[{ V[t, j], P[t, j]}, {j, n}]], 
     PlotRange -> {{0, 35}, {0, 35}}], {t, 0, i}, 
    AnimationRate -> 0.3, AnimationRunning -> True], 
   Placed[LineLegend[{Red, Green, 
      Blue}, {"Traiettoria \!\(\*SubscriptBox[\(P\), \
\(1\)]\)=(1,1,0)", 
      "Traiettoria \!\(\*SubscriptBox[\(P\), \(2\)]\)=(1,2,0)", 
      "Traiettoria \!\(\*SubscriptBox[\(P\), \(3\)]\)=(1,3,0)"}, 
     LegendLabel -> Placed["TRAIETTORIE", Above], 
     LegendFunction -> "Frame"], Below]];(*Animazione del moto*)
\[Xi][s_, t_, j_] := Exp[(2 t s)/(t^2 + 1) + Log[x[t, j]]]
\[Eta][s_, t_, j_] := Exp[s/(t + 1) + Log[y[t, j]]]
\[CapitalLambda][s_, t_, 
  j_] := {\[Xi][s, t, j], \[Eta][s, t, 
   j]}(*Linea di corrente del punto j-esimo all'istante t*)
Streakline[t_, j_] := 
 Piecewise[{{ParametricPlot[\[CapitalLambda][s, t, j], {s, 0, 3}, 
     PlotRange -> {{-10, 10}, {-10, 10}}, ImageSize -> Large, 
     AspectRatio -> 1, 
     PlotStyle -> 
      RGBColor[0.98, 0.85, 0.37]], (t - 0)*(t - 1)*(t - 2) != 
     0}, {ParametricPlot[\[CapitalLambda][s, t, j], {s, 0, 3}, 
     PlotRange -> {{-10, 10}, {-10, 10}}, ImageSize -> Large, 
     AspectRatio -> 1, 
     PlotStyle -> 
      Piecewise[{{Red, t == 0}, {Green, t == 1}, {Blue, 
         t == 2}}]], (t - 0)*(t - 1)*(t - 2) == 0}}]
StreaklineMotion[j_, i_] := 
 Legended[Animate[
   Show[Table[Streakline[h - 1, j], {h, 3}], 
    Trajectory[j, i, 0, 0, 0, 0.5, 1], Streakline[t, j], 
    Graphics[{ V[t, j], P[t, j]}], 
    PlotRange -> {{0, 35}, {0, 35}}], {t, 0, i}, 
   AnimationRate -> 0.12, AnimationRunning -> True], 
  Placed[LineLegend[{Red, Green, Blue, 
     RGBColor[0.98, 0.85, 
      0.37]}, {"Linea di corrente di \!\(\*SubscriptBox[\(P\), \(1\)]\
\) al tempo t=0", 
     "Linea di corrente di \!\(\*SubscriptBox[\(P\), \(1\)]\) al \
tempo t=1 ", 
     "Linea di corrente di \!\(\*SubscriptBox[\(P\), \(1\)]\) al \
tempo t=2", 
     "Linea di corrente di \!\(\*SubscriptBox[\(P\), \(1\)]\) al \
tempo t"}, 
    LegendLabel -> 
     Placed["LINEE DI CORRENTE DI \!\(\*SubscriptBox[\(P\), \
\(1\)]\)=(1,1,0)", Above], LegendFunction -> "Frame"], 
   Below]](*Animazione linee di corrente del j-esimo punto*)
Row[{MotionPoints[3, 5], , , , StreaklineMotion[1, 2]}]

So could someone help me, please?

$\endgroup$
8
  • $\begingroup$ Replace Below with {0.5, 0.7} for both legends. p.s. your code does not compute, but I think it is computing at your end just fine. EDIT: It could be version dependent. I have 12.2.0. Animation is running but inside a red box. $\endgroup$
    – Syed
    Nov 24, 2021 at 8:42
  • 1
    $\begingroup$ m = Animate[ Legended[ Show[Table[Trajectory[j, i, 0, 0, 0, 0, 0], {j, n}], Graphics[Table[{V[t, j], P[t, j]}, {j, n}]], PlotRange -> {{0, 35}, {0, 35}}], Placed[LineLegend[{Red, Green, Blue}, {"Traiettoria \!\(\*SubscriptBox[\(P\), \ \(1\)]\)=(1,1,0)", "Traiettoria \!\(\*SubscriptBox[\(P\), \(2\)]\)=(1,2,0)", "Traiettoria \!\(\*SubscriptBox[\(P\), \(3\)]\)=(1,3,0)"}, LegendLabel -> Placed["TRAIETTORIE", Above], LegendFunction -> "Frame"], {0.5, 0.7}]], {t, 0, i}, AnimationRate -> 0.3, AnimationRunning -> True]; $\endgroup$
    – Syed
    Nov 24, 2021 at 9:21
  • 1
    $\begingroup$ Make this change: Move Animate outside and Legended inside in both places, then Export["C:\\test.gif", m] and the same thing for n or similar variable. Use a quick time player to see the animation or similar. $\endgroup$
    – Syed
    Nov 24, 2021 at 9:23
  • 1
    $\begingroup$ Really fine: it works perfectly. Thanks very much for the assistance!!! $\endgroup$ Nov 24, 2021 at 9:50
  • 1
    $\begingroup$ @Syed Since your approach seems to have solved the OP's problem, please turn it into an answer! $\endgroup$
    – MarcoB
    Nov 24, 2021 at 11:44

1 Answer 1

1
$\begingroup$
(*Ascissa iniziale del j-esimo punto*)
Subscript[x, 0][j_] := 1 

(*Ordinata iniziale del j-esimo punto*)
Subscript[y, 0][j_] := j 

(*Legge orraria del j-esimo punto sull'asse delle ascisse*)
x[t_, j_] := Subscript[x, 0][j] (t^2 + 1)

(*Legge oraria del j-esimo punto sull'asse delle ordinate*)
y[t_, j_] := Subscript[y, 0][j] (t + 1)

(*Traiettoria del j-esimo punto*)
T[t_, j_] := {x[t, j], y[t, j]}

(*Lista colori*)
color = {Red, Green, Blue};
Trajectory[j_, i_, r_, g_, b_, o_, s_] := Piecewise[
  {
   {ParametricPlot[T[t, j], {t, 0, i}
     , ImageSize -> Large
     , AspectRatio -> 1
     , PlotStyle -> Extract[color, j]], s == 0
    }
   , {ParametricPlot[T[t, j], {t, 0, i}
     , ImageSize -> Large
     , AspectRatio -> 1
     , PlotStyle -> {RGBColor[r, g, b]
       , Directive[Opacity[o]]}], s == 1}}
  ]
(*Punto j-esimo*)
P[t_, j_] := {PointSize[0.01], Point[T[t, j]]}
(*Vettore velocità della j-esima traiettoria al tempo t*)

V[t_, j_] = {Arrowheads[0.03],
   Arrow[{T[t, j], T[t, j] + {D[x[t, j], t], D[y[t, j], t]}}]
   };

MotionPoints[n_, i_] := (mm = Animate[Legended[Show[Table[
        Trajectory[j, i, 0, 0, 0, 0, 0], {j, n}],
       Graphics[Table[{V[t, j], P[t, j]}, {j, n}]],
       PlotRange -> {{0, 35}, {0, 35}}
       ]
      , Placed[LineLegend[{Red, Green, Blue}
        , {"Traiettoria \!\(\*SubscriptBox[\(P\), \(1\)]\)=(1,1,0)"
         , "Traiettoria \!\(\*SubscriptBox[\(P\), \(2\)]\)=(1,2,0)"
         , "Traiettoria \!\(\*SubscriptBox[\(P\), \(3\)]\)=(1,3,0)"}
        , LegendLabel -> Placed["TRAIETTORIE", Above]
        , LegendFunction -> "Frame"], {0.5, 0.7}]]
     , {t, 0, i}
     , AnimationRate -> 0.3
     , AnimationRunning -> True]);

(*Animazione del moto*)
\[Xi][s_, t_, j_] := Exp[(2 t s)/(t^2 + 1) + Log[x[t, j]]]
\[Eta][s_, t_, j_] := Exp[s/(t + 1) + Log[y[t, j]]]

(*Linea di corrente del punto j-esimo all'istante t*)
\[CapitalLambda][s_, t_, j_] := {\[Xi][s, t, j], \[Eta][s, t, j]}

Streakline[t_, j_] := Piecewise[
  {{ParametricPlot[\[CapitalLambda][s, t, j], {s, 0, 3}
     , PlotRange -> {{-10, 10}, {-10, 10}}
     , ImageSize -> Large, AspectRatio -> 1
     , PlotStyle -> 
      RGBColor[0.98, 0.85, 0.37]], (t - 0)*(t - 1)*(t - 2) != 0}, {
    ParametricPlot[\[CapitalLambda][s, t, j], {s, 0, 3}
     , PlotRange -> {{-10, 10}, {-10, 10}}
     , ImageSize -> Large, AspectRatio -> 1
     , PlotStyle -> Piecewise[{
        {Red, t == 0}
        , {Green, t == 1}
        , {Blue, t == 2}}]]
    , (t - 0)*(t - 1)*(t - 2) == 0}}
  ]

StreaklineMotion[j_, i_] := (nn = Animate[Legended[Show[Table[
       Streakline[h - 1, j], {h, 3}]
      , Trajectory[j, i, 0, 0, 0, 0.5, 1]
      , Streakline[t, j]
      , Graphics[{V[t, j], P[t, j]}]
      , PlotRange -> {{0, 35}, {0, 35}}
      ]
     , Placed[LineLegend[{Red, Green, Blue, RGBColor[0.98, 0.85, 0.37]}
       , {"Linea di corrente di \!\(\*SubscriptBox[\(P\), \(1\)]\) al \
tempo t=0"
        , 
        "Linea di corrente di \!\(\*SubscriptBox[\(P\), \(1\)]\) al \
tempo t=1 "
        , 
        "Linea di corrente di \!\(\*SubscriptBox[\(P\), \(1\)]\) al \
tempo t=2"
        , 
        "Linea di corrente di \!\(\*SubscriptBox[\(P\), \(1\)]\) al \
tempo t"}
       , LegendLabel -> Placed[
         "LINEE DI CORRENTE DI \!\(\*SubscriptBox[\(P\), \
\(1\)]\)=(1,1,0)", Above]
       , LegendFunction -> "Frame"], {0.5, 0.7}]]
    , {t, 0, i}
    , AnimationRate -> 0.12
    , AnimationRunning -> True])
(*Animazione linee di corrente del j-esimo punto*)
Row[{MotionPoints[3, 5], Spacer[8], StreaklineMotion[1, 2]}]

For the export:

Export["C:\\test_mm.gif", mm]
Export["C:\\test_nn.gif", nn]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.