Message Boards Message Boards

0
|
4237 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Dynamic in the middle of Show

n = 1;
    Slider[Dynamic[n], {0, 2 Pi}]
    Show[
     Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Black],
     Dynamic[Plot[Sin[x], {x, 0, n}, 
       PlotStyle -> Red]] (* Only this line *)
     ]

I want to run Dynamic exclusively in the second graph. I do not know where to put the instruction Dynamic in that line.

POSTED BY: Ernesto Espinosa
6 Replies

You can eliminate the update issue by evaluating the static plot outside Dynamics as follows

n = 1;
m = Plot[Sin[1/x^2], {x, -2 Pi, 2 Pi}, PlotStyle -> Black];
Animator[Dynamic[n], {0.01, 2 Pi}]
Dynamic@Show[m, Plot[Sin[x], {x, 0, n}, PlotStyle -> Red]]

Side note: Show picks plot attributes (PlotRange, etc...) from the first plot. If this is not what you really want, You can override this, or change the order of the plots inside Show.

POSTED BY: Nasser M. Abbasi

I want to thank Nasser M. Abbassi for this dialogue with me.

It made me reflect on small mistakes commited by me.

Thanks Nasser.

POSTED BY: Ernesto Espinosa
n = 1;
Animator[Dynamic[n], {0.01, 2 Pi}]
Dynamic@Show[Plot[Sin[1/x^2], {x, -2 Pi , 2 Pi}, PlotStyle -> Black],
  Plot[Sin[x], {x, 0, n}, PlotStyle -> Red]
  ]

I send a concrete example.

When I run the Animator, the first graph distorts his output and returns to normal when I stop the Animator.

This is uncomfortable for a presentation.

If I can not use Dynamic in Show, and I will see how I solve this dilemma.

It would be very convenient to have the flexibility to choose which function to use Dynamic separate, in execution.

Thanks again. Best regards

POSTED BY: Ernesto Espinosa

You can use DynamicModule

DynamicModule[{n = 1},
 Row[{Slider[Dynamic[n], {0, 2 Pi}],
   Dynamic@Show[Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Black],Plot[Sin[x], {x, 0, n}, PlotStyle -> Red]]}]
 ]

Or better :) use Manipulate

Manipulate[
 Show[
  Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Black],Plot[Sin[x], {x, 0, n}, PlotStyle -> Red]],
 {{n, 1, "n"}, 0, 2 Pi, Appearance -> "Labeled"}
 ]

enter image description here

POSTED BY: Nasser M. Abbasi

Maybe I was not clear.

I just wish that the Dynamic affect the second Plot inside the Show.

In the examples provided, Dynamic affects both Plots.

I want to optimize the code, in case the first Plot doesn't need Dynamic.

Thank you for your attention

POSTED BY: Ernesto Espinosa

But Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Black] has no n in it. So it does not depend on dynamics of n changing at all. If you show an example of what is it you are trying to optimize, it will help.

i.e. it is the same as if you wrote

DynamicModule[{n = 1, m = Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Black]},
Row[{Slider[Dynamic[n], {0, 2 Pi}],
Dynamic@Show[m,Plot[Sin[x], {x, 0, n}, PlotStyle -> Red]]}]]

Dynamic can not be inside Show, since it is not graphics object, so has to be outside Show.

But again, if you show an example where it matters, it will help, since in your example, the first plot has no dynamics in it, so it will not affect the performance.

POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract