Group Abstract Group Abstract

Message Boards Message Boards

0
|
8.9K Views
|
6 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Pass on options to ListPlot

Posted 12 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]
POSTED BY: Jeff Burns
6 Replies
Posted 2 years ago
POSTED BY: Eric Rimbey
Posted 2 years 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 BY: Eric Rimbey
Options[f] = Options[ListPlot];

f[x_, opts : OptionsPattern[]] := ListPlot[x, FilterRules[{opts}, Options[ListPlot]]]
POSTED BY: David Reiss

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 BY: Chase Turner

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 BY: Chase Turner
POSTED BY: Chase Turner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard