MathGroup Archive 2002

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

Search the Archive

Re: creating functions with variable number of options

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33131] Re: creating functions with variable number of options
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Tue, 5 Mar 2002 03:08:32 -0500 (EST)
  • References: <a5t29o$662$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Okke,
I have assumed that you want to be able to overide the defaults for xPlot.
I have also allowed for the different syntaxes possible for the value of
PlotStyle and for several expressions being plotted at the same time.

Options[xPlot] = {PlotStyle ->
     {{Thickness[0.02], Hue[0]}}};

xPlot[expr_, range_, (opts___)?OptionQ] :=
  Module[{pst, def, new},
   pst = PlotStyle /. {opts} /. PlotStyle -> {{}};
    def = PlotStyle /. Options[xPlot];
    new = pst /. {{x___} :> (Flatten[{def, #1}] & ) /@
         {x}, x_ :> {Flatten[{def, x}]}};
    Plot[expr, range, PlotStyle -> new, opts]]

xPlot[x, {x, 0, 1}];

xPlot[x, {x, 0, 1}, PlotStyle -> {Hue[0.4]}];

The default can be modified independently for each of several expressions

xPlot[{x, x^2}, {x, 0, 1}];

xPlot[{x, x^2}, {x, 0, 1}, PlotStyle ->
    {Hue[0.4], Thickness[0.01]}];

--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Okke" <Okke at NOSPAMtref.nl> wrote in message
news:a5t29o$662$1 at smc.vnet.net...
> Hello,
>
> I want to create a function with an option to set the plot color.
> After looking at the examples I was cabable of making a function
> in which I could add an option, but I couldnt merge the additional
> option with the used option inside the function.
>
> could somebody please help me and tell me how I can make the function
> and function call below work?
>
> tia,
>
>
> xPlot[function_, range_] :=
>   Plot[function, range,PlotStyle -> {Thickness[0.02]}]
>
> xPlot[Sin[n], {n, 0, 2 Pi}, PlotStyle -> {RGBColor[0, 1, 0]}]
>
>
> --
> Okke
>
> remove NOSPAM from my email address to reply
>




  • Prev by Date: Re: Plotting from a file
  • Next by Date: Re: something fishy in mathematica random generation?
  • Previous by thread: Re: creating functions with variable number of options
  • Next by thread: Quirk in MathLink for Excel