2
$\begingroup$

Instead of writing a package and importing it, at the beginning of many of my .nb files I have a string of SetOptions commands which govern things like plot styling. However, when I subsequently make a CDF based on elements using those options, the CDF doesn't incorporate them. Is there an easy way to fix that?

SetOptions[Plot,
  AxesStyle -> Directive[25, Thickness[.005]], 
  PlotStyle -> {Thickness[.005]},
  ImageSize -> 550
];
Manipulate[
  Plot[x^e, {x, 0, 1}, PlotRange -> {{0, 1}, {0, 1}}, AxesOrigin -> {0, 0}],
  {e, 0, 5}
]

Thanks!

$\endgroup$
3
  • $\begingroup$ Additionally why not just put the options directly into Plot? $\endgroup$ Aug 3, 2013 at 22:57
  • $\begingroup$ The problem is that I usually have several plots in the Manipulate[.], so it gets very redundant. $\endgroup$
    – Anastasius
    Aug 3, 2013 at 23:56
  • $\begingroup$ The Initialization:> method didn't seem to work either (I stored the options as opts= and then did Initialization:>opts, and nothing :/) $\endgroup$
    – Anastasius
    Aug 3, 2013 at 23:57

1 Answer 1

2
$\begingroup$

Three methods...

Initialization

Nasser's suggestion to put SetOptions in Initialization works for me:

Manipulate[
 Column[{
   Plot[x^e, {x, 0, 1}, PlotRange -> {{0, 1}, {0, 1}}, AxesOrigin -> {0, 0}],
   Plot[Sin[e x], {x, 0, 1}, PlotRange -> {{0, 1}, {0, 1}}, AxesOrigin -> {0, 0}]
   }],
 {e, 0, 5},
 Initialization :> (SetOptions[Plot, 
     AxesStyle -> Directive[25, Thickness[.005]], 
     PlotStyle -> {Thickness[.005]}, ImageSize -> 250];)
 ]

Use With to insert options

Here the options are stored in one place, so if you need to modify them, it's easy.

Manipulate[
 With[{myPlotOpts = {AxesStyle -> Directive[25, Thickness[.005]], 
     PlotStyle -> {Red, Thickness[.005]}, ImageSize -> 250}},
  Column[{
    Plot[x^e, {x, 0, 1}, PlotRange -> {{0, 1}, {0, 1}},
      AxesOrigin -> {0, 0}, myPlotOpts],
    Plot[Sin[e x], {x, 0, 1}, PlotRange -> {{0, 1}, {0, 1}},
      AxesOrigin -> {0, 0}, myPlotOpts]
    }]
  ],
 {e, 0, 5}]

Using a custom Plot

The function myPlot adds the desired options to the arguments args passed to Plot.

SetAttributes[myPlot, HoldAll]
myPlot[args__] := Plot[args, AxesStyle -> Directive[25, Thickness[.005]], 
   PlotStyle -> {Red, Thickness[.005]}, ImageSize -> 250];

Manipulate[
 Column[{
   myPlot[x^e, {x, 0, 1}, PlotRange -> {{0, 1}, {0, 1}}, AxesOrigin -> {0, 0}],
   myPlot[Sin[e x], {x, 0, 1}, PlotRange -> {{0, 1}, {0, 1}}, AxesOrigin -> {0, 0}]
   }],
 {e, 0, 5},
 SaveDefinitions -> True]

Output

All the outputs look the same. (I changed the styling to make the differences more visually obvious.)

Mathematica graphics


Additional considerations

The third method has the advantage that if there are several Manipulates in the CDF, they all have the same styling. With the first two, those methods would need to be repeated in each Manipulate. (You cannot be sure that if SetOptions occurs in just one Manipulate, it will be executed before the other Manipulates are updated.) Also SetOptions affects the options globally. They are stored in the Kernel. So having it in one CDF will affect other CDFs that are open.

$\endgroup$
1
  • $\begingroup$ Thanks, I do not know what I've done that I thought it is not working. :) $\endgroup$
    – Kuba
    Aug 5, 2013 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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