1
$\begingroup$

I want to redefine a function similar to Plot like this:

newPlot[func_, range_, prop___] := Plot[func, range, prop, GridLines -> something, 
                                        FrameStyle -> something]

To be more flexible, I'd like to write it more in the following way:

moreProp = GridLines -> something, FrameStyle -> something
newPlot[func_, range_, prop___] := Plot[func, range, prop, moreProp]

Unfortunately it's not working. What's the necessary syntax than for moreProp

I just edited the question for more clarification (I hope).

$\endgroup$
  • 1
    $\begingroup$ SetOptions ... ? $\endgroup$ – BlacKow Apr 8 '16 at 19:36
  • $\begingroup$ Not really what I was looking for $\endgroup$ – Kay Apr 8 '16 at 19:45
  • $\begingroup$ Use moreProp = {GridLines -> something, FrameStyle -> something} $\endgroup$ – bbgodfrey Apr 8 '16 at 19:58
  • $\begingroup$ I tried that, it's not working this way I guess... $\endgroup$ – Kay Apr 8 '16 at 20:03
  • 1
    $\begingroup$ @Kay moreprops = {Frame -> True, GridLines -> Automatic}; newPlot[func_, range_, prop___] := With[{mp = moreprops}, Plot[func, range, prop, mp]]; newPlot[Sin[x], {x, 0, 10}, GridLinesStyle -> Dashed] still works $\endgroup$ – BlacKow Apr 8 '16 at 20:07
3
$\begingroup$

This is one aspect of a classic problem: Plot is HoldAll. So, moreProp is not being evaluated. I think the most effective way to work around this is to use With (cf. this question) to inject the values into Plot, e.g.

newPlot[func_, range_, prop___] := 
 With[{mp = moreProp}, Plot[func, range, prop, mp]]

Then, it works:

enter image description here

Alternatively, you can use

newPlot2[func_, range_, prop___] := Plot[func, range, prop, #] & @ moreProp

which performs a similar injection.

As was pointed out in the comments, Plot accepts lists of options, so the above methods work. But, if Plot did not do this, you could use one of the following two methods:

newPlot3[func_, range_, prop__] := 
 With[{mp = Sequence@@Flatten[{prop, moreProp}]},
  Plot[func, range, mp]
 ]

or

newPlot4[func_, range_, prop__] := 
  Plot[func, range, prop, ##]& @@ Flatten[{moreProp}]
$\endgroup$
  • $\begingroup$ Seems is not working, when moreProp consists of more than one property like GridLines -> something, FrameStyle -> something, etc. $\endgroup$ – Kay Apr 8 '16 at 19:52
  • 1
    $\begingroup$ @Kay newPlot[func_, range_, prop___] := With[{mp = {Frame -> True, GridLines -> Automatic}}, Plot[func, range, prop, mp]]; newPlot[Sin[x], {x, 0, 10}, GridLinesStyle -> Dashed] works for me. $\endgroup$ – BlacKow Apr 8 '16 at 19:55
  • $\begingroup$ @BlacKow Indeed, you are right. I forgot to cramp the options... However, I'm not happy with the fact, that I have to assign dozens of Options to every redefiniton. Therefore I was hoping to do it like to assign something to a variable... I just edited my question to be a bit more clear hopefully $\endgroup$ – Kay Apr 8 '16 at 20:01
  • 1
    $\begingroup$ @Kay you can reset what moreProp is set to. Try moreProp = {GridLines -> Automatic, FrameStyle -> Blue, Frame -> {{True, False}, {False, True}}} and then moreProp = {PlotStyle -> Red} and run newPlot[Sin[x], {x, 0, 10}] after each change. You'll see that it updates the values. $\endgroup$ – rcollyer Apr 8 '16 at 20:10
  • $\begingroup$ Luckily Plot[] accepts a list of options; for some other functions, using Sequence[] might be necessary. $\endgroup$ – J. M. will be back soon Apr 8 '16 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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