Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question
    
Additionally why not just put the options directly into Plot? –  Mike Honeychurch Aug 3 '13 at 22:57
    
The problem is that I usually have several plots in the Manipulate[.], so it gets very redundant. –  Anastasius Aug 3 '13 at 23:56
    
The Initialization:> method didn't seem to work either (I stored the options as opts= and then did Initialization:>opts, and nothing :/) –  Anastasius Aug 3 '13 at 23:57
add comment

1 Answer 1

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.

share|improve this answer
    
Thanks, I do not know what I've done that I thought it is not working. :) –  Kuba Aug 5 '13 at 5:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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