# Pass on options to ListPlot

**URL:** <https://community.wolfram.com/t/pass-on-options-to-listplot/1819>\
**Category:** Questions\
**Tags:** graphics-and-visualization\
**Created:** [March 28, 2014, 1:09pm UTC](https://community.wolfram.com/t/pass-on-options-to-listplot/1819 "2014-03-28T13:09:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@jeff](https://community.wolfram.com/u/jeff)\
**Post date:** [March 28, 2014, 1:09pm UTC](https://community.wolfram.com/t/pass-on-options-to-listplot/1819/1 "2014-03-28T13:09:44Z")

</div>

Why are the options not getting to ListPlot?

Options = Flatten[{Flatten[Options /@ {ListPlot}, 1]}, 1];[/code]

```auto
f[x_, opts : OptionsPattern[]] := 
     Module[
      {},
      Print[Evaluate[FilterRules[{opts}, Options[ListPlot]]]];
      ListPlot[x,
       PlotStyle -> Evaluate[FilterRules[{opts}, Options[ListPlot]]] 
       ]
      ]

```

```auto
f[{1, 3, 5}]

```

```auto
f[{1, 3, 5}, Joined -> True, PlotStyle -> Red]

```

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [March 28, 2014, 4:42pm UTC](https://community.wolfram.com/t/pass-on-options-to-listplot/1819/2 "2014-03-28T16:42:29Z")

</div>

```auto
Options[f] = Options[ListPlot];

f[x_, opts : OptionsPattern[]] := ListPlot[x, FilterRules[{opts}, Options[ListPlot]]]

```

---

<div class="post-metadata">

**Author:** ![chase](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/chase/32/19787_2.png) [@chase](https://community.wolfram.com/u/chase)\
**Post date:** [August 6, 2023, 1:00pm UTC](https://community.wolfram.com/t/pass-on-options-to-listplot/1819/3 "2023-08-06T13:00:59Z")

</div>

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]

```

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [August 6, 2023, 3:05pm UTC](https://community.wolfram.com/t/pass-on-options-to-listplot/1819/4 "2023-08-06T15:05:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chase](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/chase/32/19787_2.png) [@chase](https://community.wolfram.com/u/chase)\
**Post date:** [August 6, 2023, 5:03pm UTC](https://community.wolfram.com/t/pass-on-options-to-listplot/1819/5 "2023-08-06T17:03:02Z")

</div>

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]]

```

---

<div class="post-metadata">

**Author:** ![chase](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/chase/32/19787_2.png) [@chase](https://community.wolfram.com/u/chase)\
**Post date:** [August 7, 2023, 12:46am UTC](https://community.wolfram.com/t/pass-on-options-to-listplot/1819/6 "2023-08-07T00:46:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [August 7, 2023, 2:50pm UTC](https://community.wolfram.com/t/pass-on-options-to-listplot/1819/7 "2023-08-07T14:50:56Z")

</div>

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.
