2
$\begingroup$

What do these different parameters such as "#$3$" and "&" in ColorFunction command mean and how to use them? Thank you.

Plot3D[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2}, 
 ColorFunction -> (ColorData["TemperatureMap"][#3] &)]
$\endgroup$
1
  • $\begingroup$ The same as Plot3D[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2}, ColorFunction -> Function[{x, y, z}, ColorData["TemperatureMap"][z]]] $\endgroup$
    – cvgmt
    Oct 6 at 5:58

1 Answer 1

5
$\begingroup$

& is syntactic sugar for Function. In a function definition #n refers to the nth argument. So, an expression like (#1^#2)& is literally equivalent to Function[#1^#2]. You can apply such functions to arguments in the normal way, with []. So, (#1^#2) &[a, b] and Function[#1^#2][a, b] should each give you Power[a, b].

The ColorFunction option allows you to provide your own function for how to color a plot. Plot3D is designed with its own mechanism for passing the coordinates of points to the color function (a default value for ColorFunction exists, so this process occurs for every Plot3D evaluation whether or not you provide the option explicitly). So, in the example ColorFunction -> (ColorData["TemperatureMap"][#3] &), the specified color function only cares about the 3rd coordinate. If you look at the resulting plot, you can see that color depends only on the vertical coordinate. For fun, compare the results you get by substituting ColorFunction -> (ColorData["TemperatureMap"][#2] &); it should now depend only on the "y" coordinate.

Side note: the parentheses are important. The precedence rules would interpret ColorFunction -> ColorData["TemperatureMap"][#2] & as Function[ColorFunction -> ColorData["TemperatureMap"][#2]], which is not a valid option. We want ColorFunction -> Function[ColorData["TemperatureMap"][#2]].

ColorData is a special function that produces functions. It can produce many different functions, and one of them can be specified by "TemperatureMap". Having produced a function, you can apply it to an argument: ColorData["TemperatureMap"][.75] (this example should give RGBColor[0.955963, 0.863115, 0.283425]).

All of this is well-documented, and you should try to find answers in the documentation before posting in this forum. Sometimes putting it all together can be confusing while you're just starting with the Mathematica language, so I went ahead and tried to spell it all out for this example.

$\endgroup$
1
  • $\begingroup$ Thank you so much! $\endgroup$
    – Anaya
    Oct 6 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.