2
$\begingroup$

How can I set the default of the plot command in another part of the notebook?

if I have the following plots:

Plot[x , {x, 0, 15}, PlotRange -> All, Frame -> True, ImageSize -> 400, 
 FrameLabel -> {Style["x", Black, FontSize -> 18, FontFamily -> "Times"], Style["y=x)", Black, FontSize -> 18, FontFamily -> "Times"]}, 
 GridLines -> Automatic]

Plot[x^2 , {x, 0, 15}, PlotRange -> All, Frame -> True, ImageSize -> 400, 
     FrameLabel -> {Style["x", Black, FontSize -> 18, FontFamily -> "Times"], Style["y=x^2", Black, FontSize -> 18, FontFamily -> "Times"]}, 
     GridLines -> Automatic]

how can I make it appear:

settings = ......;
   Plot[x , {x, 0, 15}, settings];
   Plot[x^2 , {x, 0, 15}, settings];

In a way that I could change the name of the label?

$\endgroup$
4
  • $\begingroup$ by defining settings[lbl_]:=...? $\endgroup$
    – Kuba
    Feb 14, 2018 at 8:53
  • $\begingroup$ settings = Sequence[PlotRange -> All, Frame -> True, ImageSize -> 400, FrameLabel -> {Style["x", Black, FontSize -> 18, FontFamily -> "Times"], Style["y=x)", Black, FontSize -> 18, FontFamily -> "Times"]}, GridLines -> Automatic]? $\endgroup$ Feb 14, 2018 at 8:55
  • $\begingroup$ @Kuba &@ HenrikSchumacher: both does not work when I try to plot: ""Options expected (instead of plotset) beyond position 2 in Plot[.....] An option must be a rule or a list of rules" $\endgroup$
    – jarhead
    Feb 14, 2018 at 8:59
  • 1
    $\begingroup$ jarhead, you need to use it as Plot[x, {x, 0, 15}, Evaluate@settings]. $\endgroup$
    – kglr
    Feb 14, 2018 at 9:05

1 Answer 1

3
$\begingroup$
settings = {PlotRange -> All, Frame -> True, ImageSize -> 400, 
    FrameLabel -> {Style["x", Black, FontSize -> 18, FontFamily -> "Times"], 
     Style[#, Black, FontSize -> 18, FontFamily -> "Times"]}, 
    GridLines -> Automatic} &;

Plot[x, {x, 0, 15}, Evaluate[settings["y = x "]]]

enter image description here

Plot[x^2, {x, 0, 15}, Evaluate[settings["y = " <> ToString[x^2, TraditionalForm]]]]

enter image description here

Update: You can also temporarily set the options for Plot:

Internal`InheritedBlock[{Plot}, 
 SetOptions[Plot, settings["y = x"]];
 Plot[x, {x, 0, 15}]]

first picture above

Internal`InheritedBlock[{Plot}, 
 SetOptions[Plot, settings["y = " <> ToString[x^2, TraditionalForm]]];
 Plot[x^2, {x, 0, 15}]]

second picture above

$\endgroup$
4
  • $\begingroup$ Thank you, I'll accept this answer, however, Can it be written more elagantly? without the evaluate@? $\endgroup$
    – jarhead
    Feb 14, 2018 at 9:35
  • 1
    $\begingroup$ @jarhead, my pleasure. You mean Evaluate[settings["y = " <> ToString[x^2, TraditionalForm]]] instead of Evaluate@settings[...]? $\endgroup$
    – kglr
    Feb 14, 2018 at 9:44
  • $\begingroup$ I mean in general, having just setting[arg1,arg2] $\endgroup$
    – jarhead
    Feb 14, 2018 at 10:03
  • 1
    $\begingroup$ @jarhead - The Evaluate is needed becausePlothas the attribute HoldAll. You can define a new function, say myPlot, that automates all of this for you. $\endgroup$
    – Bob Hanlon
    Feb 14, 2018 at 13:37

Your Answer

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

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