# What's the argument of ColorFunction in ListContourPlot?

The documentation says it's the:

$f$ contour levels

but I'm not sure what it means. My data is of the form:

data = {{x1, y1, f1}, {x2, y2, f2}, ...}


I'd expect that the argument supplied would be the value of $f$ at the point, or the triple $(x,y,f)$. But after some try and error, I think it's neither.

• Isn't it effectively your $f_i$? Sep 30, 2016 at 20:25
• @BlacKow Nope. I did a test with integers $f$, and I'm getting non-integer values at ColorFunction.
– a06e
Sep 30, 2016 at 20:33
• Because it interpolates your $f(x,y)$ and the it chooses at what value of $z$ (which is $f$) to draw a contour line. If it chooses non-integer, it tries to call your ColorFunction for non-integer. Sep 30, 2016 at 20:35
• I presume you're aware of ColorFunctionScaling? Oct 2, 2016 at 4:21
• You can specify what $z$ -values you want to use for contours: Contours -> {0,1,2,3,...} Oct 4, 2016 at 4:03

data = Flatten[#, 1] &@
Table[{x, y, 10 Exp[-x^2 - y^2]}, {x, -2, 2, 0.1}, {y, -2, 2, 0.1}];


Our color function will give red for even numbers, blue for odd and black for non-integers.

colFun[z_] := If[z == Round[z], #, Black] &@If[EvenQ@Round@z, Red, Blue];


Then the following code gives black only for outside, it means that the ColorFunction is sampled at integers only.

ListContourPlot[data, ColorFunction -> colFun,
Contours -> Range[0.5, 10, 1], ColorFunctionScaling -> False]


If you remove the Contours option you will get just black square. Also you can check it with @george2079's code:

Reap[ListContourPlot[data, Contours -> Range[0.5, 10, 1],
ColorFunctionScaling -> False, ColorFunction :> (Sow[{##}] &)]][[2,1]]
(* {{0.00335463}, {1.}, {2.}, {3.}, {4.}, {5.}, {6.}, {7.}, {8.}, {9.},{10.}} *)


this is useful to see what you are getting..

data = Flatten[Table[ {i, j, 100 Sin[i j] }, {i, 0, 5}, {j, 0, 5}], 1];
Reap[ListContourPlot[ data, ColorFunction :> (Sow[{##}] &)]][[2, 1]]


{{0.}, {0.132899}, {0.235553}, {0.338208}, {0.440862}, {0.543517}, {0.646171}, {0.748826}, {0.85148}, {1.}}

Reap[ListContourPlot[ data, ColorFunctionScaling -> False,
ColorFunction :> (Sow[{##}] &)]][[2, 1]]


{{-95.8924}, {-70.}, {-50.}, {-30.}, {-10.}, {10.}, {30.}, {50.}, {70.}, {98.9358}}

note I did Sow[{##}] just in case we were being fed a sequence, but in this case its just the one value.

You can see in the unscaled case the values given to the color function are the binned values of the data, and in the default case the values are scaled to the range 0-1, which makes sense so your color function can be something like Hue

• Note that I set ColorFunctionScaling -> False, and still the values fed to ColorFunction are not the function values $f_i$.
– a06e
Oct 3, 2016 at 13:12
• right, they are not function values. The data range is divided up into nice even increments and each increment range gets a color, hence a call to ColorFunction. Note the ColorFunction is only called about 10 times regardless of the number of data points. Oct 3, 2016 at 13:16