Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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

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]

enter image description here

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]]]
share|improve this answer
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;


enter image description here

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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