0
$\begingroup$

I would like to use ListLinePlot with data from excel file but it does not work! However, ListPlot is working fine?! Below is the code I used

colorBar[arg_] := Blend[{Red, Green, Blue}, Rescale[arg, {-1, 1}]];
postE = Table[{x, 0.05 + Cos[x]^2, Cos[x]}, {x, 0, 30, 0.05}];  
ListLinePlot[{Style[{#[[1]], #[[2]]}, colorBar[#[[3]]]] & /@ postE}]   
ListPlot[{Style[{#[[1]], #[[2]]}, colorBar[#[[3]]]] & /@ postE}]  
$\endgroup$
  • 1
    $\begingroup$ What is the definition of colorBar? $\endgroup$ – Bob Hanlon Oct 19 '17 at 23:11
  • $\begingroup$ @BobHanlon I added it to the code $\endgroup$ – HD2006 Oct 19 '17 at 23:27
  • $\begingroup$ Please define "but it does not work!". Are there error messages? Also, you'd certainly need to define colorBar prior to using it. Moving the colorBar definition to the beginning of your code above produces a perfectly fine graph. $\endgroup$ – JimB Oct 19 '17 at 23:36
  • $\begingroup$ @JimB, ListLinePlot gives only one color but I need the color to be as in the ListPlot $\endgroup$ – HD2006 Oct 19 '17 at 23:41
  • 1
    $\begingroup$ But that's what you need to describe in your question. "it does not work!" doesn't explain anything. $\endgroup$ – JimB Oct 20 '17 at 0:21
3
$\begingroup$

The color of the line in ListLinePlot is controlled by the option ColorFunction

postE = Table[{x, 0.05 + Cos[x]^2, Cos[x]}, {x, 0, 30, 0.05}];

colorBar[arg_] := Blend[{Red, Green, Blue}, Rescale[arg, {-1, 1}]]

If you know the third element is Cos[x] then

ListLinePlot[postE[[All, 1 ;; 2]],
 ColorFunction -> Function[{x, y}, colorBar[Cos[x]]],
 ColorFunctionScaling -> False]

enter image description here

If you don't know the functional value then use

ListLinePlot[postE[[All, 1 ;; 2]],
 ColorFunction -> 
  Function[{x, y}, colorBar[postE[[Position[postE[[All, 1]], x][[1, 1]], 3]]]],
 ColorFunctionScaling -> False]

enter image description here

$\endgroup$
  • $\begingroup$ Thanx a lot @Bob $\endgroup$ – HD2006 Oct 20 '17 at 0:58

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.