Message Boards Message Boards

0
|
85 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to control the color of line segments in ListLinePlot?

I have read the relevant documentation. It is known that ListLinePlot can control the line's color through x-axis or y-axis data. However, I need to control the line's color based on additional data. Sample data or code is as follows:

data = Table[{x, Sin[x], Cos[x]}, {x, 0, 10, 0.1}];
ListLinePlot[data[[All, {1, 2}]]]

enter image description here

I hope to control the color of the line segment based on data[[All,3]]. For example, when the value is greater than or equal to 0, it is red; when it is less than 0, it is green. How to do this?

I tried another way, but I couldn't reconnect the line.

Show[Map[
  ListLinePlot[#[[All, {1, 2}]], 
      PlotStyle -> If[#[[1, 3]] >= 0, Red, Blue]] &@# &, 
  SplitBy[data, #[[3]] >= 0 &]], PlotRange -> All]

enter image description here

POSTED BY: Tsai Ming-Chou
2 Replies

Thank you very much!
You provided me with such a good idea~~
Completely meets my needs.

data = Table[{x, Sin[x], Cos[x]}, {x, 0, 10, 0.1}];
colF = Interpolation[data[[All, {1, 3}]]];
ListLinePlot[data[[All, {1, 2}]], 
 ColorFunction -> Function[{x, y}, If[colF[x] <= 0.1, Red, Blue]], 
 ColorFunctionScaling -> False]

enter image description here

POSTED BY: Tsai Ming-Chou

Assuming you only have discrete data and no access tot the function that generates the data,

colF = Interpolation[data[[All, {1, 3}]]];
ListLinePlot[data[[All, {1, 2}]], 
 ColorFunction -> Function[{x}, If[colF[x] <= 0, Red, Blue]], 
 ColorFunctionScaling -> False]

with the function available i would use Plot instead

Plot[Sin[x], {x, 0, 10}, 
 ColorFunction -> Function[{x}, If[Cos[x] <= 0, Red, Blue]], 
 ColorFunctionScaling -> False]
POSTED BY: Martijn Froeling
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract