Message Boards Message Boards

0
|
6152 Views
|
9 Replies
|
6 Total Likes
View groups...
Share
Share this post:

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 :D

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!

POSTED BY: Robert Poenaru
9 Replies

Hey there Ian,

That is actually an extremely useful tip. Thank you!

POSTED BY: Robert Poenaru

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,

enter image description here

A notebook implementation of the above is attached.

Hope this helps,

Ian

Attachments:
POSTED BY: Ian Williams

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*)
POSTED BY: Martijn Froeling

Could you use?

SetOptions[Plot, AspectRatio->0.8, Frame->True, ...]
POSTED BY: Ian Williams
Posted 4 years ago

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

Documentation

POSTED BY: Hans Milton
Posted 4 years ago

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"]]

enter image description here

POSTED BY: Rohit Namjoshi

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?

POSTED BY: Robert Poenaru
Posted 4 years ago

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.

POSTED BY: Rohit Namjoshi

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

POSTED BY: Robert Poenaru
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract