Pass list of options to Plot3D
Lets see the Attributes
of Plot3D
Attributes[Plot3D]
{HoldAll, Protected, ReadProtected}
The HoldAll
is the reason why your options
are not read within the Plot3D
command. In such situations (e.g NIntegrate, Plot,..) to resolve the values of a symbol (here options
) within these commands we tend to use Evaluate
to forcefully override this attribute.
So one solution is
Plot3D[x y, {x, 0, 10}, {y, 0, 10},Evaluate@plotOptions]
Another solution is to inject the options using With
:
With[{iPlotOpts = plotOptions},
Plot3D[x y, {x, 0, 10}, {y, 0, 10}, iPlotOpts]
]
Another way will be to get rid of this HoldAll
globally using ClearAttributes[Plot3D, HoldAll]
. Then you can just use Plot3D[x y , {x, 0, 10}, {y, 0, 10}, plotOptions]
. But clearing attributes globally is a bad idea in general, as it disrupts the local scoping which is often a must for a function like Plot3D
to work seamlessly!
Try for example:
ClearAttributes[Plot3D, HoldAll]; x = 5; y = 10;
Plot3D[x y, {x, 0, 10}, {y, 0, 10}]
There are other easy ways to inject the evaluated plotOptions
into Plot3D
. One very simple way is to use With
. Here, the fundamental difference between With
and Module
comes to light: With
does not localize variables. It assigns values to names and you can refer to your values by using the names:
With[{plotOptions = {PlotPoints -> 10, MaxRecursion -> 2}},
Plot3D[x y, {x, 0, 10}, {y, 0, 10}, plotOptions]
]
Another way is to make a function from your Plot3D
call and pass your options as parameters
Plot3D[x y, {x, 0, 10}, {y, 0, 10}, #] &[{PlotPoints -> 10, MaxRecursion -> 2}]
In this way, you can of course remove the list braces
Plot3D[x y, {x, 0, 10}, {y, 0, 10}, ##] &[PlotPoints -> 10, MaxRecursion -> 2]
I can't comment on halirutan's answer, so this will have to suffice: the problem with his last suggestion is that
Plot3D[x y, {x, 0, 10}, {y, 0, 10}, #] &[PlotPoints -> 10, MaxRecursion -> 2]
is equivalent to
Plot3D[x y, {x, 0, 10}, {y, 0, 10}, PlotPoints -> 10]
i.e., only the first option setting is inserted. The proper way to go about it is to use SlotSequence
(##
) instead; to wit,
Plot3D[x y, {x, 0, 10}, {y, 0, 10}, ##] &[PlotPoints -> 10, MaxRecursion -> 2]
and both options shall then be inserted.
If you have an entire pile of options to insert, you could do something like
optList = {ColorFunction -> Hue, MaxRecursion -> 2, Mesh -> False, PlotPoints -> 10};
Plot3D[x y, {x, 0, 10}, {y, 0, 10}, ##] & @@ optList