1
$\begingroup$

Here is the code:

Dynamic@Show[{Graphics[{LightGray, Rectangle[{0, -1}, {Pi, 1}]}], 
   Plot[Sin[a x], {x, 0, Pi}]}]
Slider[Dynamic[a]]

The Slider controls the value of a in Plot[] to produce a dynamic plot of Sin[]. I would like to make the Plot[] dynamic only, but it didn't work if i only wrapped plot[] with dynamic[], so i added dynamic to the whole show[]. So how to dynamically update plot[] solely?

$\endgroup$
4
  • $\begingroup$ Manipulate[Plot[Sin[a x], {x, 0, π}, PlotRange -> {{0, π}, {-1, 1}}, Prolog -> {LightGray, Rectangle[{0, -1}, {π, 1}]}], {{a, 1}, -2, 2, 0.01, Appearance -> "Labeled"}] $\endgroup$ – Bob Hanlon Sep 19 '20 at 4:46
  • $\begingroup$ the code i posted in the question is a simplified example, the original code has combined graphics and plot, so it's unlikely to modify it by using Prolog. $\endgroup$ – DORA Sep 19 '20 at 4:59
  • $\begingroup$ Besides, the Prolog will be updated as well while the value of a changes, right? Because the whole Plot is dynamic. $\endgroup$ – DORA Sep 19 '20 at 5:06
  • $\begingroup$ Slider[Dynamic[a]] gr = Graphics[{LightGray, Rectangle[{0, -1}, {Pi, 1}]}]; Dynamic[Show[gr, Plot[Sin[a x], {x, 0, Pi}]]] $\endgroup$ – Bob Hanlon Sep 19 '20 at 5:24
1
$\begingroup$

You can evaluate the Graphics expression in place, so that it just becomes a constant in the Show expression. Like so:

stuff

$\endgroup$

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.