Re: Set default options for a function
- To: mathgroup at smc.vnet.net
- Subject: [mg59257] Re: Set default options for a function
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 3 Aug 2005 01:19:59 -0400 (EDT)
- Organization: The Open University, Milton Keynes, U.K.
- References: <dcmu2e$gi9$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Daniele Lupo wrote: > Hi. > > I've created (after some effort), a function that plot into a Smith chart a > set of data. > > In this function, all works OK, but I'd like to set default options for > MultipleListPlot in a different way. > > My function in made in this way: > > MultipleListSmith[lista_List,options___] := > Module[{listaUV}, > .....I modify listaUV here... > MultipleListPlot[listaUV, options] > ] > > now, my data by default is created by four sets of xy data > {{data1},{data2},{data3},{data4}}, so I must to do four plots with > MultipleListPlot. > > I't works all (now...), but I'd like to create, by default, four different > styles for every plot, in this way: > > SymbolShape->None, PlotStyle->{{Red},{Blue},{Green},{Orange}}, > PlotJoined->True > > I'd like that these options are used by default, changing them when I need > other styles (considering that lista represents four data sets, by > default). > I've used the default value > > options___:{PlotJoined -> True, SymbolShape -> None, PlotStyle -> {{Red}, > {Blue}, {Green}, {Orange}}} > > But it does not work, returning the plot with default options, not mine... > > I've also tried the example shown in the mathematica book of the online > help 2.3.10, so, I've written: > > Options[MultipleListSmith] = {PlotJoined -> True, SymbolShape -> None, > PlotStyle -> {{Red}, {Blue}, {Green}, {Orange}}}; > > MultipleListSmith[lista_List, options___]:= > Module[ > ...... > MultipleListPlot[ > listaUV, > PlotStyle /. {options} /. Options[MultipleListSmith], > PlotJoined /. {options} /. Options[MultipleListSmith], > SymbolShape /. {options} /. Options[MultipleListSmith] > ] > ] > > but it returns errors to me, of this type: > > MultipleListPlot::badargs : > MultipleListPlot has been given bad arguments. Input Should be data sets > consisting of points or points with ErrorBar specifications. > > If I replace my last three arguments with options, all works fine, but I > don't have dafault values that I wants... > > Anyone can help me? > > Thanks for your answers > > Daniele > Hi Daniel, One way of doing what you want is illustrated below. It might not be the most efficient or elegant way of achieving this but it works. Indeed, the example 2.3.10 from the Mathematica book works because the function fn expect a series of numeric agreements and the chain of replacement rules (default and/or user defined) for the options yields a numeric value when the function fn is called. However, in your case, what we want is to modify the replacement rules that are going to be passed to the function *MultipleListPlot*, therefore we must find a way of stopping the last evaluation for each option. For instance, assume that the default value for the option SymbolShape is None and the user wants Automatic. The sequence SymbolShape /. {options} /. Options[MultipleListSmith] is replaced by SymbolShape /. SymbolShape -> Automatic /. SymbolShape -> None and is evaluated from right to left as ((SymbolShape /. SymbolShape -> None) /. SynbolShape -> Automatic) expression that yields the rule SynbolShape -> Automatic which is evaluated as Automatic This is why *MultipleListPlot* complains about bad arguments because it receives only the results of the replacement rules, not the replacement rules themselves. Therefore, by altering the name of the options, you can get all the evaluation needed but the last one and a replacement rule is passed to the function rather than just the final value. In[1]:= Needs["Graphics`MultipleListPlot`"] In[2]:= (list1 = Table[{x, Sin[2*Pi*x]}, {x, 0, 1, 0.1}]; list2 = Table[{x, Cos[2*Pi*x]}, {x, 0, 1, 0.1}]); In[3]:= Clear[MultipleListSmith]; In[4]:= Options[MultipleListSmith] = {PlotJoinedValue -> True, SymbolShapeValue -> None, PlotStyleValue -> {{Red}, {Blue}, {Green}, {Orange}}}; In[5]:= MultipleListSmith[lista_List, (options___)?OptionQ] := Module[{listaUV = lista}, MultipleListPlot[listaUV, PlotStyle -> PlotStyleValue /. {options} /. Options[MultipleListSmith], PlotJoined -> PlotJoinedValue /. {options} /. Options[MultipleListSmith], SymbolShape -> SymbolShapeValue /. {options} /. Options[MultipleListSmith], options]] In[6]:= MultipleListSmith[{list1, list2}]; In[7]:= MultipleListSmith[{list1, list2}, SymbolShapeValue -> Automatic, Frame -> True]; I hope that I have been clear enough in my lengthy explanation. Best regards, /J.M.