0
|
5349 Views
|
6 Replies
|
5 Total Likes
View groups...
Share

# Pass on options to ListPlot

Posted 11 years ago
 Why are the options not getting to ListPlot? Options = Flatten[{Flatten[Options /@ {ListPlot}, 1]}, 1];f[x_, opts : OptionsPattern[]] :=      Module[      {},      Print[Evaluate[FilterRules[{opts}, Options[ListPlot]]]];      ListPlot[x,       PlotStyle -> Evaluate[FilterRules[{opts}, Options[ListPlot]]]        ]      ]f[{1, 3, 5}]f[{1, 3, 5}, Joined -> True, PlotStyle -> Red]
6 Replies
Sort By:
Posted 1 year ago
 Eric -- that is a terrific writeup -- thanks for taking the time to do that! Please check out my bio page here on the community and if you are inclined, please drop me a direct email.
Posted 11 years ago
 Options[f] = Options[ListPlot];f[x_, opts : OptionsPattern[]] := ListPlot[x, FilterRules[{opts}, Options[ListPlot]]]
Posted 1 year ago
 How would one modify pWrapH and pWrapG to work with Plot? pWrapH[opt : OptionsPattern[Plot]] := Plot[x^2, {x, 1, 5}, FilterRules[{opt}, Options[Plot]]]; pWrapH[PlotStyle -> Gray] Or pWrapG[opt : OptionsPattern[Plot]] := Block[{pArgs}, pArgs = Sequence @@ FilterRules[{opt}, Options[Plot]]; Plot[x^2, {x, 1, 5}, pArgs]]; pWrapG[PlotStyle -> Gray] 
Posted 1 year ago
 It's not clear what you're trying to achieve. You're putting in a lot of extra effort that doesn't actually accomplish anything. You could simply do this: pWrapH[opts : OptionsPattern[]] := Plot[x^2, {x, 1, 5}, opts] If you're trying to filter out invalid options before passing them to Plot, then you could do something like this: pWrapG[opts : OptionsPattern[]] := With[ {filteredOpts = Sequence @@ FilterRules[{opts}, Options[Plot]]}, Plot[x^2, {x, 1, 5}, filteredOpts]] But this could effectively mask errors, making them more difficult to debug.
Posted 1 year ago
 Eric -- I had not previously considered using With[...] with Plot -- thank you! But I don't see the "why" of Plot requiring the use of a With[...] to evaluate an expression with a temporary variable value, whereas if I substituted ListPlot[xyDataList] for Plot[...] in either pWrapH and pWrapG, ListPlot won't throw a runtime error due to my using a local Block variable. And on the topic as to why I'm creating a wrapper function.... I'd certainly welcome a pointer to a Mathematica Developer's Guidebook or Tech Paper as to how I can better support my medical researchers whose data visualization requirements do not entirely align with Mathematica's Graphics defaults. In particular, Medical researchers do not want the overhead of "fiddling" with all the Optional Graphics properties; rather, they prefer to call my graphic wrapper functions by passing in a single argument -- their data -- and from that, I extract relevant properties to prepare as Options to underlying Mathematica APIs. Of course, there are occasions where there is a need for an over-ride and hence, the pWrap examples I supplied are tiny examples to explore why Plot was being so difficult.What follows is another contrived example whose purpose is to illustrate the goal of merging different plotting options and yet, still allowing for over-rides: pWrapX[(assoc_)?AssociationQ, opt : OptionsPattern[ListPlot]] := Block[{pArgs, xyPairs, yRange, xRange, plotOpts, maxCube}, (*BEGIN: Contrived Plot Default Properties*) xyPairs = assoc["Data"]; (*Always Plot a Cube*) maxCube = 1.1 Max@Abs@Flatten@xyPairs; {xRange, yRange} = {{-maxCube, maxCube}, {-maxCube, maxCube}}; (*Collect over-rides*) pArgs = FilterRules[{opt}, Options[ListPlot]]; (*Combine user-supplied optionals with defaults at the end*) plotOpts = Join[pArgs, {PlotRange -> {xRange, yRange}, ImageSize -> Large, AspectRatio -> 1, PlotTheme -> "Detailed", PlotLabel -> "Subject:" <> assoc["SubjectID"]}]; (*END: Contrived Plot Default Properties*) ListPlot[xyPairs, Sequence @@ plotOpts]]; Block[{data}, data = <| "Data" -> ({#, 5 Cos[(2 Pi) #/2 + Pi]} & /@ Range[-5, 5, .001]), "SubjectID" -> "LA01", "Period" -> 2|>; pWrapX[data, PlotStyle -> Orange, ImageSize -> Small]] 
Posted 1 year ago
 The reason why With works and Block does not has to do with the sequence of evaluation and the HoldAll attribute of Plot. Because of the HoldAll attribute, Plot gets to decide when it wants to evaluate any of its arguments. This can be confusing, but the confusion is usually around the first argument, the "body" of the Plot. People will create an expression, save it as a variable, pass that variable to Plot, and get unexpected results or errors. This is because Plot only sees that variable until it decides to "unpack" it, and it's at that moment that things start being evaluated. In your case, the confusion is coming with the options.Let's look one of your examples: pWrapG[opt : OptionsPattern[Plot]] := Block[{pArgs}, pArgs = Sequence @@ FilterRules[{opt}, Options[Plot]]; Plot[x^2, {x, 1, 5}, pArgs]] Sure, pArgs is now a local variable and is assigned a value, but the expression Plot[x^2, {x, 1, 5}, pArgs] still "sees" aArgs, the literal symbol, because Plot has the HoldAll attribute. I don't know the implementation details of Plot, but apparently it expects options to be a list of rules that it can inspect directly without needing to evaluate anything first. One fix would be to interrupt the normal sequence of evaluation and force the symbol to be evaluated before Plot "gets its hands on it": Plot[x^2, {x, 1, 5}, Evaluate[pArgs]] Okay, so why did it work with With? Because With also has the HoldAll attribute, and is designed to work in a particular way. It evaluates all of the local variables in its first argument and does the specified replacements on the main body before anything in the body evaluates. The With effectively disappears, and it's as if those local variables never existed. So in my example pWrapG[opts : OptionsPattern[]] := With[ {filteredOpts = Sequence @@ FilterRules[{opts}, Options[Plot]]}, Plot[x^2, {x, 1, 5}, filteredOpts]] Plot never sees filteredOpts, it only ever sees the expression assigned to filteredOpts, which is a sequence of rules as desired.Now, having said all of that, ListPlot does not have the HoldAll attribute. So you don't need to play these games with ListPlot. For your pWrapX example, we could just do this: pWrapX[(assoc_)?AssociationQ, opt : OptionsPattern[ListPlot]] := With[ {xyPairs = Lookup[assoc, "Data", {}], subjectIdForLabel = "Subject: " <> Lookup[assoc, "SubjectID", "default ID"]}, With[ {plotRange = (1.1 Max@Abs@Flatten@xyPairs) {{-1, 1}, {-1, 1}}}, ListPlot[ xyPairs, opt, ImageSize -> Large, AspectRatio -> 1, PlotTheme -> "Detailed", PlotLabel -> subjectIdForLabel, PlotRange -> plotRange]]] Usage: data = <|"Data" -> ({#, 5 Cos[(2 Pi) #/2 + Pi]} & /@ Range[-5, 5, .001]), "SubjectID" -> "LA01", "Period" -> 2|>; pWrapX[data] And any override options you provide will take precedence just because of the way options are handled.