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.

  • 3
    $\begingroup$ Perhaps supply some sample data? $\endgroup$ – ciao Jun 8 '14 at 22:55
  • 1
    $\begingroup$ Think of using ListContourPlot. it may help you viewing the data more efficiently $\endgroup$ – Algohi Jun 8 '14 at 23:51
  • $\begingroup$ Related question here $\endgroup$ – 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]]]
  • 1
    $\begingroup$ 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. $\endgroup$ – Jens Jun 9 '14 at 17:49
  • $\begingroup$ @Jens:Ah! Thanks for the addition - bookmarked your linked post! $\endgroup$ – 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

  • $\begingroup$ 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. $\endgroup$ – Jens Jun 9 '14 at 20:35
  • $\begingroup$ @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. $\endgroup$ – eldo Jun 9 '14 at 21:14

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.