ListLinePlot with colors from data

I am trying to color a simple 2D plot (using ListLinePlot) based on data in the form of 3-vectors.

ListLinePlot[data[[All, {1, 2}]]]

I would like to use one of the ColorData color schemes (like “Temperature”) for the line color, depending on this third value in my table 3-vectors. That is,Data[[All, 3]] will be the source of the color-coding values.




Perhaps supply some sample data?
– ciao
Jun 8 ’14 at 22:55



Think of using ListContourPlot. it may help you viewing the data more efficiently
– Algohi
Jun 8 ’14 at 23:51



Related question here
– m_goldberg
Jun 9 ’14 at 1:10


2 Answers


I think something like this is what you’re after (I created some data for this example):

data = Range@20;
tbl = Transpose[{data, data^3, data/25}];

ListLinePlot[tbl[[All, {1, 2}]],
ColorFunction -> Function[{x, y}, Darker[ColorData[“Temperature”][tbl[[Round[x], 3]]], .2]],
ColorFunctionScaling -> False]

Setting the ColorFunctionScaling to False gets you the X value which is then used to index back into your data to get the desired index of the color scheme. Note that if your actual X values are not integers, you’ll need to use other means (lookup, or an incrementing counter) to index into the desired color. Here’s an example of that:

colors = tbl[[All, 3]]l
cnt = 1;

ListLinePlot[tbl[[All, {1, 2}]],
ColorFunction -> Function[{x, y}, Darker[ColorData[“Temperature”][colors[[cnt++]]], .2]]]



This is how it should work (+1), but there is one pesky issue: as soon as VertexColors appear in the plot (which this approach uses behind the scenes), the output is no longer anti-aliased. This can make the lines appear ugly and jagged if you look closely. This has been addressed in How can I prevent ColorFunction from disabling antialiasing in graphics where I wrap the plot output in a function removeVertexCol to get rid of the VertexColors.
– Jens
Jun 9 ’14 at 17:49



@Jens:Ah! Thanks for the addition – bookmarked your linked post!
– ciao
Jun 9 ’14 at 20:45

data = Table[{Cos[k 2 Pi/7.], Sin[k 2 Pi/7.], k/21.}, {k, 0, 21, 3}];

colors = VertexColors -> (Hue /@ Last /@ data)

plot = ListLinePlot[Most /@ data, AspectRatio -> 1, ColorFunction -> Function[{x, y}, Hue@y]];

plot[[1, 2, 3]] = colors;




For me this doesn’t work (version 8). It’s a little more robust if you use a replacement rule to look directly for VertexColors: for example, plot /. HoldPattern[VertexColors -> Automatic] :> colors works for me. But you do lose the anti-aliasing as I mentioned below @Kuba’s answer, too.
– Jens
Jun 9 ’14 at 20:35



@Jens – In 9.01 it works, but my example is, of course, more than ugly. Meanwhile I learnt by self-teaching, how to plug in complicated functions via ColorFunction into a ParametricPlot to color it , for example, according to its curvature. Only being 50% satisfied with my results so far, I’ll ask a similar question to SE in a couple of days. Hopefully, you will read it.
– eldo
Jun 9 ’14 at 21:14