# How does ListLinePlot's application of ColorFunction work?

(I'm using V12.1)

There are a lot of questions on how to use ColorFunction in many plotting functions, but I have not seen this problem mentioned in any of them, nor do the methods I have found seem to solve this issue.

It seems that there's an issue occurring with how exactly ListLinePlot is working with the data that is being plotted.

I'd like to note that using such forms as ylist below, one can find success in actually coloring the plot according to values in a second list, however, if you use such forms as xylist, there seem to be some serious issues that cause the ColorFunction to cease working as it should.

xylist = {{{1, 2}, {2, 4}, {3, 8}}, {{1, 1}, {2, 2}, {3, 3}}};
ylist = {{2, 4, 8}, {1, 2, 3}};
zlist = {{6, 5, 4}, {3, 2, 1}};
ListLinePlot[ylist, ColorFunction -> Function[{x, y}, Print[{x, y}]],
ColorFunctionScaling -> False];
ListLinePlot[ylist, ColorFunction -> Function[{x, y}, Print[{y}]],
ColorFunctionScaling -> False];
ListLinePlot[xylist, ColorFunction -> Function[{x, y}, Print[{x, y}]],
ColorFunctionScaling -> False];
ListLinePlot[xylist, ColorFunction -> Function[{x, y}, Print[{y}]],
ColorFunctionScaling -> False];
ListLinePlot[xylist,
ColorFunction ->
Function[{x, y}, Print[zlist[[##]] &@Position[xylist, {x, y}]]],
ColorFunctionScaling -> False];
ListLinePlot[xylist,
ColorFunction -> Function[{x, y}, Print[Position[xylist, {x, y}]]],
ColorFunctionScaling -> False];
ListLinePlot[ylist,
ColorFunction -> Function[{x, y}, Print[Position[ylist, {y}]]],
ColorFunctionScaling -> False];


Gives this:

{1.,2.}

{2.,4.}

{3.,8.}

{1.,1.}

{2.,2.}

{3.,3.}

{1.,2.}

{2.,4.}

{3.,8.}

{1.,1.}

{2.,2.}

{3.,3.}

{2.}

{4.}

{8.}

{1.}

{2.}

{3.}

{2.}

{4.}

{8.}

{1.}

{2.}

{3.}

{1.,2.}

{2.,4.}

{3.,8.}

{1.,1.}

{2.,2.}

{3.,3.}

{1.,2.}

{2.,4.}

{3.,8.}

{1.,1.}

{2.,2.}

{3.,3.}

{2.}

{4.}

{8.}

{1.}

{2.}

{3.}

{2.}

{4.}

{8.}

{1.}

{2.}

{3.}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}

{}


# Can anyone tell me what's going wrong with ListLinePlot's use of ColorFunction here?

## Why is it parsing through the values twice?

### Why is it producing empty sets instead of what one would see below--because things like this still work as they should:

zlistfunc =
Function[{x, y},
Print[zlist[[##]] & @@ Position[xylist, {x, y}][[1]]]];
zlistfunc[2, 4]
(* 5 *)

• Can you explain exactly what problem you see, though, rather than having us figure it our from running your code? – MarcoB Apr 16 at 18:59
• I don't exactly understand what the problem is, but I'll put my output, though, it is a bit large, and I figured it wasn't necessary. Updated @MarcoB The results don't make sense even from a debugging side... – CA Trevillian Apr 16 at 19:00

I don't know why the values are passed to ColorFunction twice, but I guess that this would only be a problem if the evaluation of the ColorFunction were extremely resource intensive.
The fact that Position does not return matches as written is probably due to the fact that the values passed to ColorFunction are machine-precision, whereas they are exact arbitrary-precision integers in the xylist against which you are matching. The two are different to MMA, so Position does not find an exact match.
Try either numericizing the list (i.e. ... Position[N@ xylist, {x, y}]...) or rationalizing the values obtained by the color function (i.e. ... Position[xylist, Rationalize@ {x, y}] ...).
• Well, seems that was what solved the problem here with this MWE but not for my actual use case. This is really frustrating. I don't understand why ListLinePlot using indices is functional for my case, but not for an x-y scheme... – CA Trevillian Apr 16 at 22:32
• I use this to color a list of 50,000+ points, it's just pulling the point value from a second list and running a ColorFunction on it, so it seems like maybe I am running into issues because the list is so large? – CA Trevillian Apr 16 at 22:39
• would it be better to just use a list of x-y-z, and somehow get ListLinePlot to only use the x-y, then use Position to grab the z value, within the ColorFunction? – CA Trevillian Apr 16 at 22:52
• Yeah, something's going way wrong with ListLinePlot... – CA Trevillian Apr 17 at 0:57
• @CATrevillian two things: 1) are you sure that you need to plot 50,000 points? I doubt that the plot will be displayed on a medium with that kind of resolution anyway. 2) given your goal, it may be easier to construct the plots by hand, using styled Point or Line objects inside Graphics. – MarcoB Apr 17 at 2:19