← Back to posts

Manipulate

A set of examples on built-in functions for interactive plotting, and expression manipulations.

ManipulatePlot

An example of built-in function for interactive plotting

ManipulatePlot[Sin[w z], {z,0,10}, {w, 0, 15, 1}] (*VB[*)(FrontEndRef["4e0886cf-a697-40c0-b763-47c2f39a8a14"])(*,*)(*"1:eJxTTMoPSmNkYGAoZgESHvk5KRCeEJBwK8rPK3HNS3GtSE0uLUlMykkNVgEKm6QaWFiYJafpJppZmuuaGCQb6CaZmxnrmpgnG6UZWyZaJBqaAAB+9RVM"*)(*]VB*)

Example with two curves

ManipulatePlot[{ (*TB[*)Sum[(*|*)(*FB[*)((Sin[2π(2j - 1) x])(*,*)/(*,*)(2j-1))(*]FB*)(*|*), {(*|*)j(*|*),(*|*)1.0(*|*),(*|*)n(*|*)}](*|*)(*1:eJxTTMoPSmNiYGAoZgMSwaW5TvkVmYwgPguQCCkqTQUAeAcHBQ==*)(*]TB*), (*TB[*)Sum[(*|*)(*FB[*)((Cos[2π(2j - 1) x])(*,*)/(*,*)(2j-1))(*]FB*)(*|*), {(*|*)j(*|*),(*|*)1.0(*|*),(*|*)n(*|*)}](*|*)(*1:eJxTTMoPSmNiYGAoZgMSwaW5TvkVmYwgPguQCCkqTQUAeAcHBQ==*)(*]TB*) }, {x, -1,1}, {{n,4}, 1,7, 1}] (*VB[*)(FrontEndRef["d143568e-e9a0-478a-a7bd-ba861c41f1a2"])(*,*)(*"1:eJxTTMoPSmNkYGAoZgESHvk5KRCeEJBwK8rPK3HNS3GtSE0uLUlMykkNVgEKpxiaGJuaWaTqplomGuiamFsk6iaaJ6XoJiVamBkmmximGSYaAQCEhhXM"*)(*]VB*)

One can perform side effects using an optional parameter

Module[{r}, ManipulatePlot[{ (*TB[*)Sum[(*|*)(*FB[*)((Sin[2π(2j - 1) x])(*,*)/(*,*)(2j-1))(*]FB*)(*|*), {(*|*)j(*|*),(*|*)1.0(*|*),(*|*)n(*|*)}](*|*)(*1:eJxTTMoPSmNiYGAoZgMSwaW5TvkVmYwgPguQCCkqTQUAeAcHBQ==*)(*]TB*), (*TB[*)Sum[(*|*)(*FB[*)((Cos[2π(2j - 1) x])(*,*)/(*,*)(2j-1))(*]FB*)(*|*), {(*|*)j(*|*),(*|*)1.0(*|*),(*|*)n(*|*)}](*|*)(*1:eJxTTMoPSmNiYGAoZgMSwaW5TvkVmYwgPguQCCkqTQUAeAcHBQ==*)(*]TB*) }, {x, -1,1}, {{n,4}, 1,7, 1}, Epilog -> { Table[{ RandomColor[], Circle[RandomReal[{-1,1}, 2], r // Offload] }, {10}] }, "UpdateFunction" -> Function[n, r = (n - 1)/5.0; True ]] ]

ManipulateParametricPlot is another variation:

ManipulateParametricPlot[Exp[-x/w]{Sin[x], Cos[x]}, {x,0,4Pi}, {w,10,100}]

General Manipulate expression

Manipulate can be used on any valid Wolfram Expression. 2D/3D plots and images passed as expr or part of it will be automatically optimized using Just-in-Time (JIT) transpiler if possible. This provides an immediate mode for a user to construct dynamic interactive widgets.

Tip: For frequently changing data or complex visuals we still do recommend to use low-level building blocks such as Offload, InputRange.

Here is a basic example involving symbolics:

Manipulate[Column[{ Style[StringTemplate["`` m/s"][x], Blue], Table["🚗", {i, Floor[x/25]}]//Row }], {x,10,100}, ContinuousAction->True] (*VB[*)(FrontEndRef["d55861e8-94f6-4761-b9a1-78a934a137b5"])(*,*)(*"1:eJxTTMoPSmNkYGAoZgESHvk5KRCeEJBwK8rPK3HNS3GtSE0uLUlMykkNVgEKp5iaWpgZplroWpqkmemamJsZ6iZZJhrqmlskWhqbJBoamyeZAgB4IBT4"*)(*]VB*)

Series expansion:

Manipulate[Series[Sin[x], {x,0,n}], {n,1,10,1}] (*VB[*)(FrontEndRef["c27ff3d7-bbf1-4bb8-84e4-0f20ab2dbd15"])(*,*)(*"1:eJxTTMoPSmNkYGAoZgESHvk5KRCeEJBwK8rPK3HNS3GtSE0uLUlMykkNVgEKJxuZp6UZp5jrJiWlGeqaJCVZ6FqYpJroGqQZGSQmGaUkpRiaAgCUDhZW"*)(*]VB*)

Here is another example with Plot expression:

Manipulate[ Plot[Sin[a x + b], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4}, {{b, 0, "Phase Parameter"}, 0, 10}, ContinuousAction->True]

More complex version:

Manipulate[Plot[1.0 + Sin[w] Sin[x + w],{x,0,5Pi}, Epilog->{ Red, Point[{8.0, 1.0 + Sin[w] Sin[8.0 + w]}] }], {w,0,Pi}, ContinuousAction->True]

Another example, that solves ODE on-fly:

Manipulate[ Plot[Evaluate[ y[t] /. First[ NDSolve[ {y''[x] == -x y[x], y[0] == a, y'[0] == b}, y, {x, 0, 4}]]], {t, 0, 4}, Epilog -> {Point[{4, 1/2}], Green, Arrow[{{0, a}, {1, b + a}}], Red, Point[{0, a}]}, PlotRange -> {{0,5}, {-6,6}}], {{a, 1}, -3, 3}, {{b, 0}, -3, 3}]

3D plot example:

Manipulate[Plot3D[Sin[n x] Cos[n y], {x,-1,1}, {y,-1,1}], {n, 1, 5, 0.3}, ContinuousAction->True] (*VB[*)(FrontEndRef["3f93de61-e4a4-47f7-80f0-c62c0d1e6467"])(*,*)(*"1:eJxTTMoPSmNkYGAoZgESHvk5KRCeEJBwK8rPK3HNS3GtSE0uLUlMykkNVgEKG6dZGqekmhnqppokmuiamKeZ61oYpBnoJpsZJRukGKaamZiZAwCFBBV7"*)(*]VB*)

Image manipulations:

img = ImageResize[ExampleData[ExampleData["TestImage"] // Last], 350]; Manipulate[ ImageAdjust[img, {c,a}], {{c, 0},0,5,0.1}, {{a, 0},0,5,0.1}, ContinuousAction->True ]

Here is an example with mixed symbolics and graphics:

Manipulate[ Row[{ "m", "==", MatrixForm[m], StreamPlot[Evaluate[m . {x, y}], {x, -1, 1}, {y, -1, 1}, StreamScale -> Large, ImageSize -> Small ] }], {{m, ((*GB[*){{1(*|*),(*|*)0}(*||*),(*||*){0(*|*),(*|*)2}}(*]GB*))}, { ((*GB[*){{1(*|*),(*|*)0}(*||*),(*||*){0(*|*),(*|*)2}}(*]GB*)) -> "Nodal source", ((*GB[*){{1(*|*),(*|*)1}(*||*),(*||*){0(*|*),(*|*)1}}(*]GB*)) -> "Degenerate source", ((*GB[*){{0(*|*),(*|*)1}(*||*),(*||*){-1(*|*),(*|*)1}}(*]GB*)) -> "Spiral source", ((*GB[*){{-1(*|*),(*|*)0}(*||*),(*||*){0(*|*),(*|*)-2}}(*]GB*)) -> "Nodal sink", ((*GB[*){{-1(*|*),(*|*)1}(*||*),(*||*){0(*|*),(*|*)-1}}(*]GB*)) -> "Degenerate sink", ((*GB[*){{0(*|*),(*|*)1}(*||*),(*||*){-1(*|*),(*|*)-1}}(*]GB*)) -> "Spiral sink", ((*GB[*){{0(*|*),(*|*)1}(*||*),(*||*){-1(*|*),(*|*)0}}(*]GB*)) -> "Center", ((*GB[*){{1(*|*),(*|*)0}(*||*),(*||*){0(*|*),(*|*)-2}}(*]GB*)) -> "Saddle"}}]

Tips for better performance

If you still want to use Manipulate on some complex expressions, there are some tricks to avoid deoptimization of JIT engine

Tip:

  • Try to keep the number of curves/traces/polygons expressions the same if possible. Try not to add new entities to your graphs.
  • PerformanceGoal->"Speed" is also a good option for 3D plots.
  • Mesh->None might also help.
  • PlotRange->Full can help to avoid fragmentation of line segments
  • Avoid changing colors, opacity at runtime