Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a table with 3 columns - x position, y-position and colour. I'd like to plot these points and join them with line segments, where the colour of the line segment is determined by 3rd variable. e.g.

T=Table[{n,n,n},{n,0,100}];

I can plot just the points like this

Graphics[{Hue[#3/Length[T]], Point[{#1, #2}]} & @@@ T, Frame -> True, AspectRatio -> 1]

How can I plot line segments instead?

Thanks!

share|improve this question
    
It seems to be a duplicate but meanwhile try: {Hue[#1[[ 3]]/Length[T]], Line[{##}[[ ;; , ;; 2]]]} & @@@ Partition[T, 2, 1], –  Kuba Aug 26 '13 at 10:58
    
There are 100 points and 100 colors, which color you want to apply to line between n and n+1 point? the n.? –  Kuba Aug 26 '13 at 11:35
    
Possibly a duplicate of this question –  m_goldberg Aug 26 '13 at 15:47

2 Answers 2

up vote 6 down vote accepted
T = Table[{n, n, n}, {n, 0, 10}];  

Graphics[
 GraphicsComplex[
   T[[;; , ;; 2]],
   {Thickness@.02, {Hue[T[[#[[ 1]], 3]]/len], Line@#} & /@ Partition[Range[len], 2, 1] 
], Frame -> True, AspectRatio -> 1]

enter image description here

So that's another approach, but it will blur your colors:

len=Length @ T;

Graphics[
   GraphicsComplex[
      T[[ ;; , ;; 2]],
      {Thickness@.02, Line[Range[len], VertexColors -> (Hue[#/len] & /@ T[[ ;; , 3]])]}
                  ]
        , Frame -> True, AspectRatio -> 1]

enter image description here


Edit:

GraphicsComplex can be useful but for simple cases like here or the one I've faced today it is not a must:

T = Table[{n, n, n}, {n, 0, 10}];
len = Length@T;

Graphics[{Thickness@.02, 
          Line[T[[ All, {1, 2}]], 
               VertexColors -> (Hue[#/len] & /@ T[[;; , 3]])]}
        ]
share|improve this answer
    
That's perfect thanks! I prefer the blurred one but I didn't know if it was possible. I don't have enough reputation to upvote the answer... sorry –  octopus Aug 26 '13 at 12:10

If I understood you correctly, - there is a simpler way:

data = Table[{x, Sinc[x]}, {x, 0, 10, .5}];

ListPlot[data, 
 ColorFunction -> Hue,
 Joined -> True, 
 PlotStyle -> Thickness[.03], 
 Mesh -> All, 
 MeshStyle -> Directive[PointSize[.05], Opacity[.2]]]

enter image description here

share|improve this answer
    
as I understand, he'd explicilty give the color as 3rd argument of the individual points –  Pinguin Dirk Aug 26 '13 at 11:20
    
@PinguinDirk Yeah I thought it is a possibility. Still you never know. Additionally this is good to know and ColorFunction can be custom in case he just need to specify a simple data-dependent coloring scheme. –  Vitaliy Kaurov Aug 26 '13 at 11:46
    
perfectly understand- it is what I'd suggest to use too (I am too lazy to build up with primitives when there are built-in functions one could use) –  Pinguin Dirk Aug 26 '13 at 11:48
    
Many thanks for your answer. In reality, I need to give the colour as an extra argument. (I have a 2D trajectory and want to use the colour to track the time taken to reach each point) –  octopus Aug 26 '13 at 12:12

Your Answer

 
discard

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.