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: [mg106484] Re: [mg106466] default options, OptionsPattern[], and FilterRules[]
  • From: "David Park" <djmpark at comcast.net>
  • Date: Thu, 14 Jan 2010 05:48:20 -0500 (EST)
  • References: <6504030.1263382894200.JavaMail.root@n11>

Maybe this will work:

ClearAll[myplot]
Options[myplot] = 
 Join[{PlotRange -> {-5, 5}}, 
  FilterRules[Options[Plot], Except[PlotRange]]]; 
myplot[F_, opts : OptionsPattern[]] :=
 Module[{newopts = Sequence @@ Join[{opts}, Options[myplot]]},
  Plot[F, {x, -1, 1}, Evaluate@newopts]
  ]

myplot[Sin[x], PlotStyle -> Red]

myplot[Sin[x], PlotStyle -> Red, PlotRange -> {-1, 1}]

Perhaps there are cases where one is making a large number of graphics with
quite restricted variations. Then it might be worthwhile to define a private
myplot. But my humble advice is that in general work this is seldom the
case. There is no such thing as one-size-fits-all for graphics. Most
graphics will require special treatment for the case in hand. Giving the
best presentation of data or various mathematical concepts will usually
require a lot of custom work. Trying to define a template that will handle
it will end up being a lot of work in itself, and will often fail. I once
communicated with someone who was producing a new college calculus book and
he wanted one Mathematica statement that could be used to produce EVERY plot
in the book. I think his prototype had about 20 arguments. I don't know if
he succeeded. (I've fallen into a similar trap myself and the results were
not good!)


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  


From: Roman [mailto:rschmied at gmail.com] 

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: default options, OptionsPattern[], and FilterRules[]
  • Next by Date: Re: inequality as constraints on NDSolve
  • Previous by thread: Re: default options, OptionsPattern[], and FilterRules[]
  • Next by thread: Grid Dividers