Re: default options, OptionsPattern[], and FilterRules[]
- To: mathgroup at smc.vnet.net
- Subject: [mg106497] Re: [mg106466] default options, OptionsPattern[], and FilterRules[]
- From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
- Date: Thu, 14 Jan 2010 05:51:04 -0500 (EST)
- References: <201001131100.GAA06994@smc.vnet.net>
Hi,
don't you want to filter the Options of Plot and look what options of
myplot can be given to Plot too? And then you give to Plot either the
standart-value or your option to myplot or the value which is given in
the function-call.
Options[myplot] = {PlotRange -> {{-5, 5}, {-5, 5}}};
myplot[F_, OptionsPattern[]] :=
Module[{opts},
opts = Evaluate[(First@# -> OptionValue[First@#]) & /@
FilterRules[Options[Plot], First /@ Options[myplot]]];
Plot[F, {x, -1, 1}, Evaluate[Sequence @@ opts]]
]
when you know that all your options can be given to Plot, that you can
do
Clear[myplot];
Options[myplot] = {PlotRange -> {{-5, 5}, {-5, 5}},
PlotStyle -> Red};
myplot[F_, opts___Rule] :=
Module[{stdopts},
stdopts =
Select[Options[myplot], ! MemberQ[First /@ {opts}, First@#] &];
Print[Join[{opts}, Options[myplot]]];
Plot[F, {x, -1, 1},
Evaluate[Sequence @@ Join[{opts}, Options[myplot]]]]
]
Cheers
Patrick
On Wed, 2010-01-13 at 06:00 -0500, 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.
>
- References:
- default options, OptionsPattern[], and FilterRules[]
- From: Roman <rschmied@gmail.com>
- default options, OptionsPattern[], and FilterRules[]