15
$\begingroup$

I am using the following example code to export Manipulate to GIF:

ManToGif[man_, name_String, step_Integer] := 
 Export[name <> ".gif", 
  Import[Export["f:\\test.avi", man], "ImageList"][[1 ;; -1 ;; step]]]

man = Manipulate[Plot[Sin[x], {x, 0, end}], {end, Pi, 2 Pi, 0.1}];

ManToGif[man, "f:\\test", 1];

Is there another possibility to do that? How can I see the value of the variable end?

You see at bottom of the movie some strange black bars and also at the right edge a black vertical line, which should not be there.

enter image description here

$\endgroup$
  • $\begingroup$ Why don't you press that + toggler to open the additional control bar? It includes InputField which shows how the value updates. $\endgroup$ – e.doroskevic Aug 11 '16 at 12:55
  • 6
    $\begingroup$ Or, programmatically, {end, Pi, 2 Pi, 0.1, Appearance -> "Open"}. $\endgroup$ – Kuba Aug 11 '16 at 12:56
  • 1
    $\begingroup$ Those are not "strange black bars", those are the background. If you want to avoid showing the background you need to control the size of the plot so that it covers the same area at all time. $\endgroup$ – C. E. Aug 11 '16 at 12:59
  • $\begingroup$ closely related: How to Export this animation as a gif file $\endgroup$ – Kuba Aug 11 '16 at 13:01
  • 1
    $\begingroup$ @mrz - I still got a bit of movement in the axes until I added the option ImagePadding -> 10, now I get this. Another option would be to give ImageSize two numbers, so that the width and height are always specified. $\endgroup$ – Jason B. Aug 11 '16 at 14:16
11
$\begingroup$

You can do this outside of the manipulate box and show the variable's value as a legend. Also by using fixed image sizes and aspect ratios you can minimize the occurrence of black bars as the variables change.

First Step: Make a table of your desired Plots, I think the code here is relatively easy to understand:

Anim = Table[
   Plot[Sin[x], {x, 0, t}, PlotRangePadding -> 0.1, ImageSize -> 500, 
    AspectRatio -> 1/2, PlotLegends -> TextString@Row@{"T=", t}]
   , {t, Pi, 2 Pi, .1}
   ];

Second Step: Export it as a GIF image :)

Export["anim.gif", Anim]

Final Result:

enter image description here

Hope it's suitable for what you have in mind ;)

UPDATE

If you want it to move forward an then backward you can make tables of the animation for each direction and join them:

Anim1 = Table[
   Plot[Sin[x], {x, 0, t}, PlotRangePadding -> 0.1, ImageSize -> 500, 
    AspectRatio -> 1/2, PlotLegends -> TextString@Row@{"T=", t}]
   , {t, Pi, 2 Pi, .1}
   ];

Anim2 = Table[
   Plot[Sin[x], {x, 0, t}, PlotRangePadding -> 0.1, ImageSize -> 500, 
    AspectRatio -> 1/2, PlotLegends -> TextString@Row@{"T=", t}]
   , {t, 2 Pi, Pi, -.1}
   ];

Anim = Join[Anim1, Anim2];

enter image description here

UPDATE 2

If you want to do this with the manipulation box just add a two-dimensional picture size to your plot like this:

man = Manipulate[
  Plot[Sin[x], {x, 0, end}, 
   ImageSize -> {500, 250}], {end, Pi, 2 Pi, 0.1, 
   Appearance -> "Open"}, ContentSize -> 550]

enter image description here

Or this one (without fixing the plot range):

man = Manipulate[
  Plot[Sin[x], {x, 0, end}, ImageSize -> {500, 250}], {end, Pi, 2 Pi, 
   0.1, Appearance -> "Open"}, ContentSize -> 550]

enter image description here

$\endgroup$
  • $\begingroup$ I think you can still use manipulate and get good results if you use the fixed formatting there $\endgroup$ – Siav Josep Aug 11 '16 at 15:02
  • $\begingroup$ Thank you for your help ... this I did already, but I wanted to see the whole window of the Manipulate function - therefore my question. $\endgroup$ – mrz Aug 11 '16 at 15:34
  • $\begingroup$ would it be possible to get a complete example, with the exporting, the inclusion of the manipulate UI, and the alternating between forwards and backwards? I'm not super Mathematica savvy yet so it's taking me a while to figure out how to string to it together. basically my whole interest in Mathematica is being able to type in 3-D equations and get exactly this behavior $\endgroup$ – Joseph Garvin Jan 13 at 19:51
  • $\begingroup$ @JosephGarvin can you give an example of the 3D equation that you're interested in? $\endgroup$ – Siav Josep Feb 14 at 16:04
  • $\begingroup$ @SiavJosep: something like $z=\frac{ax}{ax+by}$ with sliders for $a$ and $b$. $\endgroup$ – Joseph Garvin Feb 20 at 5:07
3
$\begingroup$

Another solution:

ManToGif[man_, name_String, step_Integer] := 
 Export[name <> ".gif", 
  Import[Export[FileNameJoin[{NotebookDirectory[], "test.avi"}], man], 
         "ImageList"][[1 ;; -1 ;; step]]];

man2 = Manipulate[Plot[Sin[x], {x, 0, end}, PlotRange -> {{0, 2 Pi}, {-1, 1}}]
                 , {end, 0, 2 Pi, 0.1}];

ManToGif[man2, FileNameJoin[{NotebookDirectory[], "test"}], 1];

This creates an AVI and corresponding GIF animation:

enter image description here

$\endgroup$
  • $\begingroup$ For the original ManToGif, see this answer by Vitaly and its link to his community post. (The original is essentially the same as this, but with a fix for MacOS.) $\endgroup$ – Michael E2 Mar 15 '17 at 11:08

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.