1
$\begingroup$

I am experiencing a strange behavior in plotting a lot of functions. Basically, they are clipped within an horizontal range. Let try to run this code by varying nmax, it could be a problem of my specific configuration(?). I know that is not a problem about Manipulate since I can reproduce the same behavior with a static plot. Rather it seems to be a problem related to point numbers, indeed if you try to sharpen the gaussians the problem appears.

nmax = 5;
Manipulate[
DynamicModule[{rr = r[[1 ;; n]], ss = s[[1 ;; n]], 
  ww = w[[1 ;; n]]},
  Show[
    Plot[Table[
      PDF[NormalDistribution[rr[[i]], ss[[i]]], x], {i, 1, n}], {x, 0, 
    5}, PlotRange -> {{0, 5}, {0, 1.4}}, Frame -> True]
 ]
 ],
{{r, RandomReal[{0, 3}, nmax]}, ControlType -> None},
{{s, RandomReal[{0.01, 1}, nmax]}, ControlType -> None},
{{w, RandomReal[{0, 1}, nmax]}, ControlType -> None},
{{n, 1}, 1, nmax, 1},
Dynamic[
Grid[
Table[With[{i = i},
 {Row[{Slider[Dynamic[r[[i]]], {0, 5, 0.1}], Dynamic[r[[i]]]}], 
  Row[{Slider[Dynamic[s[[i]]], {0.01, 1, 0.1}], 
    Dynamic[s[[i]]]}]}
], {i, n}]
]
],
ControlPlacement -> Left
]

What do you think about?

Thank you very much

F

$\endgroup$
  • 1
    $\begingroup$ Why not post the (probably simpler) code for the static image? $\endgroup$ – Yves Klett Oct 23 '14 at 7:00
  • $\begingroup$ Because it would not be simpler than this, since it involves kernel density estimation computation, a histogram, the pdf reconstruction and its "manual" reconstruction. The code above reproduces the same effect/problem but with less code... and the solution is to give the right option in Plot[] (I guess) $\endgroup$ – Fabio Oct 23 '14 at 7:02
  • 2
    $\begingroup$ I don't understand what you mean is clipped. Can you show with an image? I run your code, and nothing strange appears to me. Some of the graphs go outside the PlotRange that is explicitly given, but that is not surprising. $\endgroup$ – mickep Oct 23 '14 at 7:22
2
$\begingroup$

This is the problem you are seeing:-

enter image description here

In your code Show is actually doing nothing, since Plot is plotting all the tabled functions (and that's where the problem is occurring). The problem is fixed by by rearranging Plot and Table so that plots are tabled, then shown with Show. I.e.

nmax = 5;
Manipulate[
 DynamicModule[{rr = r[[1 ;; n]], ss = s[[1 ;; n]], ww = w[[1 ;; n]]},
  Show[Table[Plot[PDF[NormalDistribution[rr[[i]],
       ss[[i]]], x], {x, 0, 5}, PlotRange -> {{0, 5}, {0, 1.4}}, Frame -> True],
    {i, 1, n}]]],
 {{r, RandomReal[{0, 3}, nmax]}, ControlType -> None},
 {{s, RandomReal[{0.01, 1}, nmax]}, ControlType -> None},
 {{w, RandomReal[{0, 1}, nmax]}, ControlType -> None}, {{n, 1}, 1, nmax, 1},
 Dynamic[Grid[Table[With[{i = i},
     {Row[{Slider[Dynamic[r[[i]]], {0, 5, 0.1}], Dynamic[r[[i]]]}],
      Row[{Slider[Dynamic[s[[i]]], {0.01, 1, 0.1}], 
        Dynamic[s[[i]]]}]}], {i, n}]]],
 ControlPlacement -> Left]

enter image description here

$\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.