2
$\begingroup$

In my Mathematica work flow I move often used functions into a custom package (.m file) at some point. Since I like the features of the CustomTicks package, I have a collection of custom plot functions, which unfortunately cause some trouble in a dynamic environment as demonstrated by the following standalone example:

Nearly every function in my .m file makes use of Module.

myPlot[data_, nx_] := Module[
   {ny},
   ny = 5;
   ListLinePlot[data,
    Frame -> True,
    FrameTicks -> {
      {FindDivisions[{##}, ny] &, FindDivisions[{##}, ny] &},
      {FindDivisions[{##}, nx] &, FindDivisions[{##}, nx] &}
      }]
   ];

If I use this function with Manipulate

data = {#, Sin@#} & /@ Range[0, 10, 0.1];
Manipulate[myPlot[data, nx], {nx, 2, 10, 1}]

everything works as expected as long as I move the slider, i.e., as long as the output changes. Releasing the slider results in the following error:

enter image description here

"A ticks specification in the value of FrameTicks should be None, Automatic, a function, or a list of ticks."

It seems that using Module inside of Manipulate is a bad idea and indeed replacing Module with DynamicModule solves the problem.

Now should I replace Module with DynamicModule for every .m file function, which might be used in a dynamic environment in the future? What would be a clever work flow to avoid such extra work? Shouldn't it be possible to use a dynamic environment without revising every custom function?

$\endgroup$
1
$\begingroup$

DynamicModule should be used for dynamic content. In your example you don't need to use Module at all, even for a static plot, because you are simply setting a default value for ny which can be done this way:

myPlot[data_, nx_, ny_: 5] := 
  ListLinePlot[data, Frame -> True, 
   FrameTicks -> {{FindDivisions[{##}, ny] &, 
      FindDivisions[{##}, ny] &}, {FindDivisions[{##}, nx] &, 
      FindDivisions[{##}, nx] &}}];

or by using With.

myPlot[data_, nx_] := 
  With[{ny = 5}, 
   ListLinePlot[data, Frame -> True, 
    FrameTicks -> {{FindDivisions[{##}, ny] &, 
       FindDivisions[{##}, ny] &}, {FindDivisions[{##}, nx] &, 
       FindDivisions[{##}, nx] &}}]
   ];
|improve this answer|||||
$\endgroup$
  • $\begingroup$ I am sorry, my example is misleading due to oversimplification. I am interested in functions, which require the Module or (DynamicModule?), i.e., functions in which the nx value is calculated and not just a constant or a default value. $\endgroup$ – Choronaut Mar 26 '16 at 6:29
  • $\begingroup$ @Choronaut how do you expect to get useful answers from misleading questions? State exactly what you need in your question. Until then just use DynamicModule $\endgroup$ – Mike Honeychurch Mar 26 '16 at 7:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.