5

How can I give Plot formating expressions on a separate line just like ListPlot?

When I use the following code with ListPlot, it produces a plot without any errors:

graphs = {ImageSize -> Full, Frame -> True};
ListPlot[Table[x, {x, 1, 2, .01}], graphs]

However, the same thing doesn't work for Plot:

graphs = {ImageSize -> Full, Frame -> True};
Plot[x, {x, 1, 2}, graphs]

Why? What is the simple notation change that I need to make it work?

Astor Florida
  • 1,326
  • 7
  • 21

2 Answers2

5

You can use

Plot[x, {x, 1, 2}, Evaluate@graphs]

Why?

The reason Plot[x, {x, 1, 2}, graphs] doesn't work and ListPlot[Table[x, {x, 1, 2, .01}], graphs]does is that Plot has attribute HoldAll ("all arguments (..) maintained in an unevaluated form")

Attributes[Plot]

{HoldAll, Protected, ReadProtected}

whereas ListPlot doesn't:

Attributes[ListPlot]

{Protected, ReadProtected}

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Alternatively, you can inject graph using With as in m_goldberg's answer or using Plot[x, {x, 1, 2}, #] &@graphs. – kglr Mar 13 '19 at 23:25
3

You can also use With because it makes the needed substitution before Plot sees any of its arguments.

options = {ImageSize -> Full, Frame -> True};
With[{opts = options}, Plot[x, {x, 1, 2}, opts]

plot

m_goldberg
  • 107,779
  • 16
  • 103
  • 257