1
$\begingroup$

I am stuck with creating animation of vector plot. My example code is simple

myField[x_, y_, t_] := {0, 100*Sin[2*Pi*50*t]*Cos[Pi*y]};
Animate[VectorPlot[myField[x, y, t], {x, 0, 1}, {y, -0.5, 0.5}, 
  VectorColorFunction -> "Rainbow", PlotLegends -> Automatic], {t, 0, 2/50}]

It is working nicely, except that vectors are rescaled every timestep. Result can be seen in gif animation.

enter image description here

I found this discussion, and came up with code

With[{vectorscale = {0, 100}, time = 0.08}, 
 Legended[VectorPlot[myField[x, y, time], {x, 0, 1}, {y, -0.5, 0.5}, 
   VectorColorFunction -> 
    Function[{x, y, vx, vy, n}, 
     ColorData[{"ThermometerColors", vectorscale}][Abs[myField]]], 
   VectorScale -> {0.03, Automatic, None}, 
   VectorColorFunctionScaling -> False, VectorPoints -> 25, 
   AspectRatio -> 1], 
  BarLegend[{ColorData[{"ThermometerColors", vectorscale}], 
    vectorscale}]]]

As you see, this one is not animation, but attempt to create fixed time plot with controllable color scaling. Unfortunately I am missing something very basic, because it does not work at all, I cannot even understand this result:

enter image description here

Can someone point out what am I missing?

Thank You!

$\endgroup$
2
$\begingroup$

changes:

  • added Animate

  • Dynamic on VectorPlot to not recreate Legend at each step

  • VectorColorFunction should be applied to the 5th argument n which is a norm of your field, not to the field function. You can square it if you need Abs though.


With[{vectorscale = {0, 100}}, 
 Animate[Legended[
   Dynamic@VectorPlot[myField[x, y, time], {x, 0, 1}, {y, -0.5, 0.5}, 
     VectorColorFunction -> 
      Function[{x, y, vx, vy, n}, 
       ColorData[{"ThermometerColors", vectorscale}][n]], 
     VectorScale -> {0.05, Automatic, None}, 
     VectorColorFunctionScaling -> False, VectorPoints -> 15, 
     AspectRatio -> 1], 
   BarLegend[{ColorData[{"ThermometerColors", vectorscale}], 
     vectorscale}]], {time, 0, 2/50}]]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you! Exactly what I needed $\endgroup$ – vargss Mar 3 '17 at 12:50
  • $\begingroup$ @vargss glad I could help, please take a tour $\endgroup$ – Kuba Mar 4 '17 at 19:14

Your Answer

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

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