Apply a general style to any Plot (plot template)

I do a lot of work that require data visualization, so I must perform multiple plots. I usually try to keep a consistent design, and for each plot I mostly have to copy and paste the styling into the Plot[] function. However, sometimes when I create new documents, I need to open old ones just to copy and paste the styling again. That becomes quite often time-consuming, especially when you really have to “bake” some plots and send them fast to your boss :smiley:

I was wondering if there is a way to save all my plot settings as some kind of template, and then just use Plot[x,{x,0,1}, Style->myStyle[xlabel , ylabel], where
myStyle[xlabel,ylabel] can be something like:


Frame -> True, Axes -> False, FrameStyle -> Directive[Black, Thick], 
FrameLabel -> {"xlabel",  "ylabel"}, PlotStyle -> {Red, Thick}, AspectRatio -> 0.8

Is there a solution that solves my needs?

Thank you in advance!

Hi Robert,

One way to do this

myStyle[xlabel_, ylabel_] := {
  Frame -> True,
  Axes -> False,
  FrameStyle -> Directive[Black, Thick],
  FrameLabel -> {xlabel, ylabel},
  PlotStyle -> {Red, Thick},
  AspectRatio -> 0.8
  }

Plot[x, {x, 0, 1}, Evaluate@myStyle["x", "y"]]

Hey there, thank you for the response.
I understand this solution and it does work, but this is local to my current document. If I go and create a new one, I will have to declare this function again.

What I would actually want is to save this as a general template and use it in any document I create. You think this is possible?

Predefined templates are available by the option PlotTheme. Some of them might suit your work.

Documentation

Hi Robert,

One option would be to add the style definition function to init.m. The definition is loaded when a kernel is started so it will be available in all open notebooks, not just the new ones you create.

You could also define a define custom plot theme, see this MSE post for examples.

Hey Rohit,
Thank you for your answer. I will definitely take a look at that. Also, thank you for the usefull link as well :slight_smile:

Could you use?

SetOptions[Plot, AspectRatio->0.8, Frame->True, ...]

Setting the options in a rule list should work and does in list line plot but not in plot weirdly

opts = {Frame -> True, Axes -> False, 
   FrameStyle -> Directive[Black, Thick], 
   FrameLabel -> {"xlabel", "ylabel"}, PlotStyle -> {Red, Thick}, 
   AspectRatio -> 0.8};

ListLinePlot[Table[x, {x, 0, 1, .1}], Sequence[opts]](*works*)

Plot[x, {x, 0, 1}, Sequence[opts]](*does not work*)

Plot[x, {x, 0, 1}, ##] & @@ opts(*works*)
Plot[x, {x, 0, 1}, Evaluate[Sequence[opts]]](*works*)

Why not just set the Options for Plot using SetOptions like this?

SetOptions[
  Plot, {Frame -> True, Axes -> False, 
   FrameStyle -> Directive[Black, Thick], PlotStyle -> {Red, Thick}, 
   AspectRatio -> 0.8}];

Then, when you use Plot like this,

Plot[x, {x, 0, 1}, FrameLabel -> {"x", "y"}, ImageSize->Small]

you should get this,

A notebook implementation of the above is attached.

Hope this helps,

Ian

SetPlotOptions.nb (5.29 KB)

Hey there Ian,

That is actually an extremely useful tip. Thank you!