Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: default options, OptionsPattern[], and FilterRules[]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106482] Re: default options, OptionsPattern[], and FilterRules[]
  • From: dh <dh at metrohm.com>
  • Date: Thu, 14 Jan 2010 05:47:56 -0500 (EST)
  • References: <hik947$6qo$1@smc.vnet.net>

Hi Roman,

the simplies way to do what you want is to specify:

SetOptions[Plot,PlotRange->{-5,5}]



If you nevertheless want to write your own function, you must not forget 

to give the default options in the plot command:



Options[myplot] = {PlotRange -> {-5, 5}};

myplot[F_, opts : OptionsPattern[]] :=

  Plot[F, {x, -1,

    1}, (Evaluate[

     Join[FilterRules[{opts}, Options[Plot]], Options[myplot]]])]

myplot[x]



Daniel



Roman wrote:

> dear group,

> I am trying to set up a plotting function which has a different

> default behavior than the standard one. Here's a simple example:

> 

> Options[myplot] = {PlotRange -> {-5, 5}};

> myplot[F_, opts : OptionsPattern[]] :=

>  Plot[F, {x, -1, 1}, Evaluate[FilterRules[{opts}, Options[Plot]]]]

> 

> What I would like to achieve with this is that when I don't specify

> the PlotRange option, as in

>    myplot[Sin[x]]

> then I would get a plot with a default plot range of {-5,5}.

> Unfortunately this does not work, since the call to FilterRules[] does

> not access the default rules defined in Options[myplot], but only

> those options explicitly specified in opts (i.e. none at all in this

> call example).

> 

> Since this seems like such a standard use of the Options framework of

> Mathematica, I would expect there to be pre-defined functionality for

> what I am trying to achieve. Of course I can manually merge the

> contents of the opts variable with those of Options[myplot] and

> eliminate duplicates, but I'd like to avoid such hacks if possible.

> 

> Do you know a simple solution for this problem?

> Thanks!

> Roman.

> 




  • Prev by Date: Re: Rotated text labels
  • Next by Date: Re: Re: restricting interpolating functions to be positive
  • Previous by thread: Re: default options, OptionsPattern[], and FilterRules[]
  • Next by thread: Re: default options, OptionsPattern[], and FilterRules[]